Meet canc, a complete lib for promise cancellation
In most modern async-heavy languages, cancellation is a first-class citizen. It really should be straightforward for a developer, but in JavaScript, gaining that level of control feels a lot like swimming against the current. I've spent a fair amount of time trying to address this gap. I didn't have a third-party library I could rely on, so I ended up building my own — and I suspect I'm probably…
A JavaScript library called canc has been developed to address the challenge of cancellation in promise chains. The creator, who had no suitable third-party libraries available, built the tool themselves and found it useful in their own projects for years before polishing it for public release. After a decade-long journey, the library is now in a stable state, offering an easier way to cancel tasks within promise chains.
The library allows developers to replace callback-heavy promise chains with generator-based functions, enabling cancellation simply using a call to .cancel(). The initial implementation of canc was inspired by coroutine libraries like co, as well as the async..await syntax which relies on generators and promises under the hood. However, the library diverged from Bluebird's cancellation approach and has become more tightly integrated with the native promises and async..await.
The project faced challenges during its development, including fixing bugs, addressing design flaws, and dealing with technical debt and copyright clearances. However, this period of stagnation allowed the library to mature and reach a stable state. The library has been influenced by the evolution of the JavaScript ecosystem, and recent developments like the integration of AbortSignal and improvements to TypeScript support have contributed to its current state.
The library's development has not been without its challenges, primarily due to the limitations of generator typing in the current JavaScript language. To maintain functional parity with async..await, users must use yield* instead of yield. This trade-off, while somewhat verbose, ensures the library maintains type safety for users. The library's roadmap includes several planned features, such as Web server middleware helpers, ESLint plugin support, async iterators toolbox, and compatibility with Node.js and older runtimes.
Developers are encouraged to explore the library and provide feedback, as ongoing work is being done to expand its functionality and improve the overall developer experience. The source code and documentation can be found at https://github.com/cancjs/canc.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.