No description
Find a file
2025-10-26 00:16:02 +02:00
.github/workflows Updated CI actions 2025-10-25 23:37:31 +02:00
tests Revert "Added test for #7" 2025-07-18 11:42:49 +02:00
yasync Chronos cancellation interop 2025-07-12 18:00:14 +02:00
.gitignore Chronos support 2025-07-12 03:09:00 +02:00
LICENSE Initial commit 2022-07-27 18:04:19 +03:00
README.md Chronos support 2025-07-12 03:09:00 +02:00
yasync.nim Workaround cannot be cast compilation error that appeared after nim PR 25220 2025-10-26 00:16:02 +02:00
yasync.nimble Update yasync.nimble, Specify dependency paths during compilation 2025-07-18 11:43:50 +08:00

yasync - Yet another async/await for Nim

Build Status nimble

Requires nim devel or 2.0.

  • Semantics is very close to that of Nim's std async, await and Future[T].
  • await operation doesn't cause any heap allocations (except async closures, and (mutually) recursive async calls).
  • callSoon is not used and async code is guaranteed to run atomically between awaits across call boundaries (TODO: maybe add an example)
  • Function return type is written as T, then async transforms it to Future[T] implicitly.
  • Supports cancellation of async tasks, with optional cancellation callbacks in terminal futures. Example.
  • Provides optional compatibility layer for interop with existing asyncdispatch and chronos code. Example. The library itself is completely dispatcher agnostic, and doesn't depend on neither asyncdispatch, nor chronos.

This library introduces async, await and Future[T] similar in semantics to Nim's native ones, but implements an optimization to avoid heap allocations. Consider the following sample:

proc sleep(ms: int): Future[void] = ... # Let's assume this function doesn't allocate

proc foo(a: int): int {.async.} =
  await sleep(a)
  var b = a + 1
  return b

proc bar() {.async.} =
  let a = await foo(5)
  let b = await foo(6)
  echo a + b

waitFor bar()

If we pretend that sleep doesn't allocate, the whole waitFor operation above will not do a single allocation. The optimization happens in async functions on await calls.

TODO: Describe how it works in details

TODO:

  • Nice stack traces