Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readJavaScript

Combine fetch cancellation with AbortSignal.any

Give fetch a Cancel button and a timeout with AbortSignal.any, then distinguish a user cancellation from an expired deadline.

Give a report download a Cancel button and a five-second deadline. Fetch accepts one signal, so combine cancellation and timeout into a single signal.

With AbortSignal.any, you pass fetch the combined signal and keep the controller wired to the button. The first input to abort supplies the reason.

diagram

First abort decides the reason

If the user cancels first, the combined signal keeps that reason after the deadline passes.

Use the abort reason to choose a message

Give each download its own AbortController. Call controller.abort() from the Cancel handler and pass controller.signal to downloadReport. The timeout starts inside the function, so every call starts with a fresh deadline.

export async function downloadReport(url, userSignal, deadline = 5000) {
  const timeout = AbortSignal.timeout(deadline)
  const signal = AbortSignal.any([userSignal, timeout])

  try {
    const response = await fetch(url, { signal })
    if (!response.ok) throw new Error(`HTTP ${response.status}`)
    return { status: "ready", text: await response.text() }
  } catch (error) {
    if (!signal.aborted || error !== signal.reason) throw error
    if (timeout.aborted && signal.reason === timeout.reason) {
      return { status: "timed out" }
    }
    return { status: "cancelled" }
  }
}

Against a slow endpoint, pressing Cancel first returns cancelled; leaving the request until the deadline returns timed out.

The signal covers reading the response body too. Keep the await response.text() inside the try block so a cancellation during that read takes the same path. HTTP errors and network failures can still reach the caller, which can report them as failures.

The reason check matters if the user presses Cancel near the deadline. Both source signals can end up aborted by the time the catch block runs. Compare the caught error with the combined signal’s reason to keep unrelated failures on the error path.

Start each attempt with a fresh controller

An aborted signal stays aborted. If you pass a spent controller signal into the next request, AbortSignal.any returns an aborted signal at once. If several inputs are already aborted, the first in the array supplies the reason.

Create the controller where you start an attempt, including when the reader clicks Retry. If the UI can start another download before the old one settles, associate results with the attempt that produced them. Otherwise a late cancellation message can replace the new request's status. For a request with only a deadline, pass the timeout signal on its own.