This blog post is part of What’s new in Node.js core? March 2022 edition.
The AbortSignal
API in Node.js has a new method: throwIfAborted()
If the abort signal has been triggered, it’ll throw the value of the reason
property. This saves writing boilerplate checking and handling code.
Contributed by James M Snell
Example
const ac = new AbortController();
ac.abort();
ac.signal.throwIfAborted();
// > DOMException [AbortError]: This operation was aborted
Support in Node.js
- Added: v17.3.0
Also supported by Deno, as well as some web browsers.
When can you use it in production?
Likely to be backported to v16 (Active LTS), but not yet confirmed.
Will be in v18 — initial release 19 Apr 2022, Active LTS starts 25 Oct 2022.
Related links
- Documentation: Node.js, MDN, WHATWG
- Node.js release notes: v17.3.0
- Twitter thread: Posted on Jan 28, 2022