Today, I'd like to talk about one of the standard JavaScript APIs you are likely sleeping on. It's called AbortController.
What is AbortController?
AbortController is a global class in JavaScript that you can use to abort, well, anything!
Here's how you use it:
const controller = new AbortController();
controller.signal;
controller.abort();Once you create a controller instance, you get two things:
- The
signalproperty, which is an instance ofAbortSignal. This is a pluggable part you can provide to any API to react to an abort event, and implement it accordingly. For example, providing it to afetch()request will abort the request; - The
.abort()method that, when called, triggers the abort event on thesignal. It also updates the signal to be marked as aborted.