LazyPromise
LazyPromise is an async primitive that has familiar semantics of a native promise, but supports auto-propagating cancellation, type-safe errors, and dependency injection.
Prior art#
Observable#
Observable is beautifully simple and auto-propagates cancellation. LazyPromise makes sure to keep that, but limits Observable to a single shot—you could say it’s a JavaScript cousin of a Single in RxJava. A single-shot Observable nicely complements Signals and doesn’t have synchronous reentry gotchas.
Native promise#
The native promise is eager and needs AbortController plumbing for cancellation, but in other respects its API is great. The LazyPromise API doesn’t just resemble the native promise API, but follows all its subtleties unless stated otherwise in the docs. This has a happy byproduct of making the library easier to document and learn.
Effect#
Like Effect, and as any self-respecting lazy promise should, LazyPromise supports generator syntax, type-safe errors, and dependency injection, but does so while staying simple.