Abort Controller in JavaScript

From the Fetch API cheat sheet · Advanced Features · verified Jul 2026

Abort Controller

Cancelling requests and implementing timeouts

javascript
// Basic cancellation
const controller = new AbortController()

const request = fetch(url, {
  signal: controller.signal
})

// Cancel the request
controller.abort()

// Cancel on user action
const controller = new AbortController()

const fetchButton = document.getElementById('fetch')
const cancelButton = document.getElementById('cancel')

fetchButton.addEventListener('click', async () => {
  try {
    const response = await fetch(url, {
      signal: controller.signal
    })
    const data = await response.json()
    console.log(data)
  } catch (error) {
    if (error.name === 'AbortError') {
      console.log('Request was cancelled')
    }
  }
})

cancelButton.addEventListener('click', () => {
  controller.abort()
})
🔴 Always abort requests when component unmounts
⚡ Cancel duplicate requests to save bandwidth
💡 Use singleton pattern for search/autocomplete
📌 AbortController can't be reused after abort()

More JavaScript tasks

Back to the full Fetch API cheat sheet