$ show ericelliott/speculation
The speculation library provides cancellable promises as a drop-in replacement for standard JavaScript promises. The core functionality is implemented in src/index.js, where the speculation function wraps the native Promise constructor to accept a cancellation promise. When the cancellation promise resolves, it triggers a cleanup handler before rejecting the speculation. The library also includes a wait utility (src/wait.js) that uses speculation to create cancellable delays. The implementation is self-contained, relies only on native Promises, and follows the Promise/A+ specification. Despite its small size, the solution demonstrates careful handling of edge cases like settlement before cancellation and error propagation in cancellation handlers.
The LLM identified 1 files worth highlighting for reuse:
Implements a higher-order function that adds cancellation capability to Promises without altering the standard Promise interface. Takes an executor function and a cancellation promise, returns a Promise that resolves/rejects normally unless the cancellation promise resolves first, triggering cleanup via onCancel. Properly handles settlement states to avoid post-settlement cancellation calls and propagates errors from cancellation handlers.