Dev Helpers
← Back

Throw and catch an abort reason

Throw and catch an abort reason using Node.js.

Open this example in the interactive library

Source code

import {
  setTimeout as delay,
  setImmediate as immediate,
} from "node:timers/promises";
console.log(
  (() => {
    try {
      AbortSignal.abort(new Error("cancelled")).throwIfAborted();
    } catch (error) {
      return error.message;
    }
  })(),
);

Sample output

cancelled