Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Watch a Slow Request Time Out

In this lesson, you gave the planner’s request for Keiko’s holidays in Japan a deadline, watched the deadline pass before Nager.Date’s answer arrived, and then gave the server a fair deadline that it met.

Key Points

  • A real server can take its time. Without a deadline, the await keeps waiting, and the planner can’t move on to its next step, sometimes for minutes.
  • AbortSignal.timeout() gives back a signal that aborts itself after the milliseconds you pass, with a TimeoutError as its reason. The signal only announces the stop. The work stops only if it holds the signal.
  • fetch() can hold a signal. Its options object, the one that carried the method and body to the trip board, takes a signal too.
  • The script’s deadline of 20 milliseconds is a toy, too short on purpose. In the recorded runs, Nager.Date took about a tenth of a second to answer. A real planner would give a server a few seconds. Timings vary, so on your network you might see a status, or a TypeError if the request fails.
  • The signal’s timer starts counting as the options object is built, and right after, the call to fetch() starts the request with that signal. When the deadline passes before any answer arrives, the signal aborts, and fetch(), which holds the signal, drops the request, and the planner stops waiting for the answer.
  • An abort can’t take back a request that already reached the server. If it got there, the server may still answer, but nobody reads it. Then the promise rejects with the signal’s reason.
  • fetch() rejects with the reason itself, a TimeoutError, so checking for an AbortError, as the promise timer from node:timers/promises taught you, would miss it. That timer rejects with an AbortError and keeps the TimeoutError in its cause. Only a plain .abort() from an AbortController gives fetch() an AbortError. So when the error’s name is "TimeoutError", you know a deadline passed.
  • With a deadline of five seconds, the answer arrived well inside it in the recorded run, and the status was 200. The promise from fetch() fulfilled when the headers arrived, but the deadline still covers reading the body, so a deadline that passes later can still reject the .json() call that reads the body.

The rule is that when you give fetch() a signal from AbortSignal.timeout() and the deadline passes first, fetch() stops waiting for the answer, and the signal’s TimeoutError rejects its promise. The planner now has three outcomes to tell apart:

  • A Response whose ok is false: the server answered “no.”
  • A TypeError: fetch() couldn’t complete the request, from a network failure or a bad URL.
  • A TimeoutError: no answer in time.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, the trip board answered your POST right away. So far, every server has answered quickly. A real one can take its time. Then the await keeps waiting, and the planner can’t move on to its next step, sometimes for minutes.

00:17 In this video, you’ll give one request a deadline, and watch what happens when the deadline comes first.

00:25 You’ve set a deadline with a signal before. AbortSignal.timeout() gives back a signal that aborts itself after the milliseconds you pass, with a TimeoutError as its reason.

00:37 The signal only announces the stop. The work stops only if it holds the signal.

00:43 And fetch can hold one. Its options object, the one that carried the method and body to the trip board, takes a signal too. This script asks Nager.Date for Keiko’s holidays in Japan, with a deadline of twenty milliseconds.

00:58 That’s a toy deadline, too short on purpose. In these runs, Nager.Date took about a tenth of a second to answer, so you get to watch the deadline fire. A real planner would give a server a few seconds.

01:13 On the timeline, the signal’s timer starts counting as the options object is built. Right after, the call to fetch starts the request with that signal. Fetch’s promise is pending, and the await waits on it.

01:27 Twenty milliseconds pass before any answer arrives. The signal aborts, with a TimeoutError as its reason. Fetch was holding the signal, so it hears the abort and drops the request, and the planner stops waiting for the answer.

01:42 An abort can’t take back a request that already reached the server, so if it got there, Nager.Date may still answer, though nobody reads it. Then the promise rejects, with the signal’s reason.

01:57 Run the script, and the catch block logs the error’s name and message. In this recorded run, it’s a TimeoutError: the operation was aborted due to timeout.

02:07 Timings vary, so on your network you might see a status, or a TypeError if the request fails.

02:14 That’s the signal’s own reason. If you check for an AbortError, as the promise timer taught you, you’ll miss this one. The timer rejected with an AbortError and kept the TimeoutError in its cause.

02:27 Fetch rejects with the reason itself. Only a plain abort from an AbortController gives fetch an AbortError. So when the error’s name is TimeoutError, you know a deadline passed.

02:40 Now give Nager.Date a fair deadline. Change 20 to 5000, five seconds, and run the script again. This time, the answer arrives well inside the deadline, and the status is 200.

02:54 Fetch’s promise fulfilled when the headers arrived, but the deadline still covers reading the body.

03:01 Here’s the rule: give fetch a signal from AbortSignal.timeout(), and if the deadline passes first, fetch stops waiting for the answer, and the signal’s TimeoutError rejects its promise.

03:14 So the planner now has three outcomes to tell apart. A Response that isn’t ok means the server answered no. A TypeError means fetch couldn’t complete the request, from a network failure or a bad URL.

03:28 A TimeoutError means no answer in time.

03:32 Next, in Exercise: Get Holidays and Save Weekends Safely, you’ll write the planner’s functions that fetch a country’s holidays and save a weekend to the trip board, ready for every one of those answers.

Become a Member to join the conversation.