AI basics & verificationPractical AI workflows

Does a Promise.race timeout actually stop fetch?

A timeout message may only mean that an outer promise settled first. When reviewing AI-written request code, inspect whether the underlying fetch received a cancellation signal, whether the timer was cleared and whether the server had already performed work. One “stopped” label cannot establish all three.

Developer working on code on a laptop
Illustrative photo: Alicia Christin Gerald · Unsplash LicenseStock illustration for the topic, not a screenshot, test of the pictured equipment or product recommendation.

Key Takeaways

Promise.race adopts the first settlement without automatically cancelling a losing fetch. For client cancellation, pass an AbortController signal to fetch and abort at the deadline, clearing the timer when the operation ends. Client abort does not prove that server work was cancelled or a write rolled back.

In this article

Hold a response open before timing out

The Promise.race reference separates choosing a settlement from cancelling other work. On December 1, 2026, the editor used Node.js 22.23.2 and an isolated local HTTP server with a response gate: the server accepted the request but withheld its response until the timeout branch settled.

The outer result was timeout while the response remained unfinished. Releasing the gate then allowed the original fetch to read done. This checks ordering without depending on a real website’s speed; it is not a latency, billing or cancellation test of a model provider.

Observed local sequence, with distinct states
StepObservationNot established
The timer branch wins raceOuter result: timeoutUnderlying fetch cancelled
Original response gate is releasedOriginal fetch reads doneTimeout means all work stopped
A separate signalled request is abortedClient AbortError; connection closure observedServer writes rolled back

Wire cancellation to the request

The AbortSignal documentation shows how the signal is associated with a request. Creating a controller without passing its signal does not make that association. This reference structure manages only its own deadline timer; it does not implement user cancellation, concurrency control or retries.

Include body consumption in the timed operation

async function readWithDeadline(url, milliseconds) {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), milliseconds);
  try {
    const response = await fetch(url, { signal: controller.signal });
    if (!response.ok) throw new Error(`HTTP ${response.status}`);
    return await response.text();
  } finally {
    clearTimeout(timer);
  }
}
Adapt the URL and deadline to the task. The local gate case checks race/abort boundaries, not every server’s behavior.

A disconnected client does not settle server state

In the second local case, the editor deliberately advanced an in-memory server task counter after client abort; it still reached 1. That authored server behavior demonstrates why connection state alone cannot establish business state. No real provider job or user data was involved.

For email sending, paid generation or writes, inspect the operation ID, result record and the provider’s actual cancellation and idempotency rules before retrying. Preserve an unknown outcome rather than automatically submitting the operation again after a UI timeout.

Check errors and cleanup as well

The Fetch guide distinguishes HTTP responses from network failures. A 404 or 500 can still produce a Response, so the reference checks response.ok separately. Do not classify every failure as AbortError; an AbortSignal.timeout implementation can also produce TimeoutError.

The async forEach completion guide asks which promise is awaited. The voice-task cancellation case addresses interaction state. This article checks a request signal and server boundary; retain separate evidence for those tasks.

Include quick success, timeout, HTTP failure and a response whose headers arrive before its body finishes. Create a fresh controller for each operation and clear your timer on completion. An already-aborted signal is not reusable for the next request.

Further reading & sources