Categories
Node.js Quick Tips Quick Tips

Provide context with abortSignal.reason

< 1 min read

This blog post is part of What’s new in Node.js core? March 2022 edition.

You can now specify a reason value when triggering an abort signal in Node.js, allowing you to provide context for why it was triggered. This value is made available on the abort signal’s reason property.

Contributed by James M Snell

Example: Trigger an abort signal with a reason (can be any value)

const ac = new AbortController();

ac.abort(new Error("The reason the signal was aborted."));

console.log(ac.signal.reason);

// > Error: The reason the signal was aborted.

Support in Node.js

Also supported by Deno, as well as some web browsers.

When can you use it in production?

You can use it now as it was backported to the v16 (Active LTS) release line.