TIL: Abort Controller


2 min read

Usually Async requests are not cancellable and you just have to abandon those requests.

Today I learned about the Abort Controller and it is very useful.

It has a really good support in general and if you are using Babel with CoreJS, I thing it will be transpiled anyway.

Let's take a look at an example. Let's create a simple Image Uploader in plain HTML, CSS and JS.


  <form id="imageUploadForm">
      <label for="file">Image</label>
        <input type="file" name="file" id="file" />
      <button type="submit">Start Upload</button>
      <button type="reset">Reset</button>
      <button type="button" id="stop">Stop Upload</button>

And JavaScript:

document.addEventListener('DOMContentLoaded', () => {
  const formEl = document.querySelector('#imageUploadForm')
  formEl && attachFormEvents(formEl)

const attachFormEvents = (formEl) => {
  if (!formEl) {
  const stop = formEl.querySelector('#stop')
  const controller = new AbortController()
  const signal = controller.signal

  formEl.addEventListener('submit', (e) => {
    const formData = new FormData(formEl)
    const url = 'https://1u3o3.sse.codesandbox.io/upload'
    return fetch(url, {signal, method: 'POST', body: formData})
      .then(r => r.json())

  stop && stop.addEventListener('click', (e) => {

How it works? Lets take a look at it step by step:

  1. Create an Abort Controller with new AbortController()
  2. Get the signal from the Controller with controller.signal
  3. When creating fetch, provide abort signal in the Request Configuration with key signal
  4. Call controller.abort() whenever you wanna abort the request

Let me know if you have tried the Abort Controller already.