$ account ericelliott
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.