r/javascript • • 3d ago

LazyPromise: an async primitive that has familiar semantics of a native promise, but supports auto-propagating cancellation, type-safe errors, and dependency injection

https://lazypromise.com/
4 Upvotes

7 comments sorted by

11

u/femio 3d ago

I'll ask the obvious since I didn't see it in Q&A: what's the functional difference between this and Effect?

4

u/Content-Set-1356 3d ago

I'd say the quick answer is Effect is a batteries-included framework, while LazyPromise is a tiny primitive.

•

u/cheers- 12h ago

() => new Promise(executor)

Looks lazy enough ;)

•

u/Content-Set-1356 10h ago

This does do the trick when all you need is laziness, but cancellation would require AbortController plumbing. Like with Effect or Observable, it's not so much "look, it's lazy" but "look what it can do if it's lazy".

2

u/Discordbothost 3d ago

I'd show cancellation with a search box where the user types twice before the first request finishes. A useful example would show both that the first fetch receives an abort signal and that only the second result reaches the UI. Those are two different things I'd want to verify.

3

u/Content-Set-1356 3d ago

Yeah, I like that example. With cancellation, you don't have to worry about the first fetch completing last and overwriting the result of the second fetch. It does sound like a good idea to provide this type of examples 🤔

1

u/torres_rosa670 2d ago

Also handle the aborted fetch's rejection: aborting it can reject the promise even if stale results stay out of the UI.