Skip to main content

Command Palette

Search for a command to run...

TIL: Abort Controller

Published
2 min readView as Markdown
P

I am developer from India currently based in Berlin, Germany.

I am very interested in Design, though I don't design 😉 I take care of it while making my apps.

Photography, videography and having random conversation over beer are my activities of spare time. Timelapses and hyperlapses make me wonder all the time 😊

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.

HTML:

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

And JavaScript:

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

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

  formEl.addEventListener('submit', (e) => {
    e.preventDefault()
    e.stopPropagation()
    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())
      .then(console.log)
      .catch(console.error)
  })

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

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.

C

Hi Pankaj,

Very interesting write-up, actually haven't seen this in action. What happens to the perhaps "partial" file on the server side?

Is there a way that would receive the abort?

P

Hey Daily Dev Tips

The server would not know about this abort, in my opinion, it is better to make another acknowledgement request to the server to let server know about the abort.

1

More from this blog

P

Pankaj Patel - @heypankaj_

63 posts

Sharing my learnings and thoughts on Blogging, Personal Branding and Programming