finds.dev← search

// the find

getify/CAF

★ 1,361 · JavaScript · MIT · updated Aug 2022

Cancelable Async Flows (CAF)

CAF wraps generator functions so they behave like async/await but can be externally canceled via a token. It's for JavaScript developers who've hit the wall of non-cancelable async functions and don't want to manually thread cancellation checks through every line. The CAG variant extends this to async-iterator streams.

Token-based cancellation cascades cleanly through nested CAF-wrapped calls with a single shared signal — this is genuinely hard to do correctly with vanilla promises. Integrates with the standard AbortController/AbortSignal API, so the same token can cancel a fetch() call and the generator waiting on it. The CAF.timeout() helper properly cleans up the underlying timer when the operation finishes first, which is the kind of subtle correctness most hand-rolled solutions miss. Signal combinators (signalRace, signalAll) and the tokenCycle helper cover real-world patterns without requiring you to reinvent them.

Last push was 2022 and it's built on generator-runner patterns that were a workaround before async/await existed — the library's core premise is increasingly niche now that AbortController is native and most runtimes support it directly. The CAG double-yield syntax for awaiting promises (yield yield pwait(...)) is genuinely confusing and would require careful code review from anyone unfamiliar with it. Token reuse across separate call chains produces subtle bugs that are hard to spot — the library warns about it but can't prevent it. At 1.3k stars and 49 forks, the community is thin; if you hit an edge case, you're likely reading the source yourself.

View on GitHub →

// want more like this?

We dig through GitHub every week and send a few repos picked for what you actually care about — each with an honest take like this one.

Get finds in your inbox → Search again →