Hasty Briefsbeta

Bilingual

Show HN: LazyPromise = Observable – Signals

4 months ago
  • #JavaScript
  • #Asynchronous
  • #Promise
  • LazyPromise is a lazy, cancelable alternative to Promise with typed errors and synchronous emission.
  • Key differences from Promise: lazy execution, cancelable, typed errors, synchronous emission (no microtask queue).
  • Designed with simplicity in mind, avoiding the complexity and misuse potential of RxJS Observables.
  • Avoids mandatory microtasks, giving developers full control over execution order.
  • Supports typed errors, enhancing error handling with type safety.
  • Creation involves an optional teardown function for cleanup, e.g., clearing timeouts.
  • Execution starts only upon subscription, unlike Promises which execute immediately.
  • Cancelable: if unsubscribed before resolution, the teardown function is called.
  • Once resolved or rejected, it caches the result for future subscribers.
  • API mirrors Promise but uses pipes instead of chaining, with functions like `map`, `catchRejection`, and `finalize`.
  • Utility functions `eager` and `lazy` convert between LazyPromise and Promise.
  • Includes a third 'failure' channel for untyped errors, complementing the typed error handling.
  • Designed to prevent silent failures; illegal operations throw errors.
  • Can check if a LazyPromise settled synchronously via unsubscribe handle comparison.
  • GitHub repository available for further exploration and usage.