Member-only story

This JavaScript API is more powerful than you imagine!

Beck Moulton
3 min readOct 17, 2024

--

Today, let’s talk about a powerful standard JavaScript API that may have been overlooked by you-AbortController

In the past, people mentionedAbortControllerWhen it comes to interrupt requests, examples are usually given, and even the description given by MDN is like this:

howeverAbortControllerThe ability is not limited to this,AbortControllerIt is a global class in JavaScript that can be used to terminate any asynchronous operation. The usage method is as follows:

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

We create aAbortControllerAfter the instance, two things will be obtained:

  • signalAttribute, this is aAbortSignalFor instance, we can pass it to the API that needs to be interrupted to respond to the interrupt event and handle it accordingly, for example, by passing it tofetch()The method can terminate this request;
  • .abort()Method, calling this method will triggersignalSuspend the event and mark the signal as aborted.

We can monitor through surveillanceabortEvent, then implement termination based on specific logic:

controller.signal.addEventListener('abort', () => {
//Implement termination logic
});

--

--

Beck Moulton
Beck Moulton

Written by Beck Moulton

Focus on the back-end field, do actual combat technology sharing Buy me a Coffee if You Appreciate My Hard Work https://www.buymeacoffee.com/BeckMoulton

No responses yet