ts-reset icon indicating copy to clipboard operation
ts-reset copied to clipboard

Reset `AbortSignal`'s `reason` to `unknown`

Open cassiozen opened this issue 2 years ago • 1 comments

AbortController is JavaScript's built-in cancelation interface for asynchronous requests (like fetch). When aborting, you can provide a "reason" - Typescript types reason as any:

const controller = new AbortController();
const signal = controller.signal;

const fetchData = async () => {
  try {
    const response = await fetch("some-url", { signal });
    // ...
  } catch (e) {
    if (e instanceof Error && e.name === "AbortError") {
      console.log(signal.reason.toUpperCase()); // `signal.reason` is `any`, so no complains here
    }
  }
};

fetchData();
controller.abort(3); // This will throw a TypeError because I'm calling toUpperCase

It seems like "resetting" AbortSignal's reason to unknown is a useful change that is in the spirit of this library. I'd be glad to open a PR.

cassiozen avatar Feb 23 '23 22:02 cassiozen

Yep, makes sense!

mattpocock avatar Feb 23 '23 22:02 mattpocock