TIL cancelling promises
POSTED ON:
TAGS: javascript mdn
I was debugging a strange promise statement, and I needed to force cancel the promise.
There is a cross-platform (Node, Browsers, etc) cancellation primitive called AbortController. You can use it to cancel functions that return promises rather than promises themselves.
const controller = new AbortController();
const task = new Promise((resolve, reject) => {
//...
controller.signal.addEventListener("abort", () => {
reject();
});
});
controller.abort();
via StackOverflow
The AbortController
interface represents a controller object that allows you to abort one or more Web requests as and when desired.
Related TILs
Tagged: javascript