AbortSignal.timeout()

noun

say it: “uh-BORT SIG-nul TIME-owt” /əˈbɔrt ˈsɪɡnəl ˈtaɪmaʊt/

A JavaScript method that makes a signal which cancels a request, such as a fetch() call, if it hasn't finished within the milliseconds you give it.

What is AbortSignal.timeout() in JavaScript?

AbortSignal.timeout() — Makes a signal that gives up after the number of milliseconds you pass. Hand it to fetch as signal, and a request that takes too long stops with a TimeoutError you can catch and explain to the visitor.

try {
  const response = await fetch('https://dog.ceo/api/breeds/list/all', {
    signal: AbortSignal.timeout(8000),
  });
  console.log(response.status); // 200
} catch (error) {
  console.log(error.name); // TimeoutError, if it took over 8 seconds
}

In a sentence

  • “I passed AbortSignal.timeout() with 5000 to fetch, so the dog photo request gives up after five seconds.”
  • “When AbortSignal.timeout() canceled the request, the page showed "This is taking too long. Try again?"”

Level: Journeyman · More JavaScript terms at this level

Updated