• JavaScript
  • Web API

Don't Sleep on AbortController

by Artem Zakharchenko

added on October 20, 2024 (1mo ago)

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 signal property, which is an instance of AbortSignal. 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 a fetch() request will abort the request;
  • The .abort() method that, when called, triggers the abort event on the signal. It also updates the signal to be marked as aborted.