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 Response Arrive in Two Steps

In this lesson, you watched Portugal’s holidays arrive from Nager.Date on a timeline, then timed the two steps of the same request in a real run.

Key Points

  • The call to fetch() hands back a promise right away, still pending. Then Node sends the GET request, and the first await waits on that promise.
  • The response comes back the way curl printed it, top first. The status line and headers arrive first, and that’s all fetch() waits for. Its promise fulfills with a Response whose status, ok flag and headers are already in it, so the log lines after the first await can run.
  • The body may still be on its way behind them. On a fast connection, some of it may already be here, but the Response hasn’t read any of it yet. Calling response.json() gives you a second promise. It reads the body to its end, whether it has already arrived or is still arriving, then parses it as JSON. Only then does it fulfill, with Portugal’s 17 holidays.
  • When you timed the two steps with Date.now(), the status 200 was logged after about a tenth of a second, and the body was read a few milliseconds later. These times show when your code resumes after each await, not when the bytes arrive. Your times will differ on every run, but the order stays the same. How long the second step takes depends on the body’s size and the connection.
  • For XX, the made-up country code that got a 404 back when you sorted status codes, the status 404 is logged after the first await too. Its status line arrives first, and its body, the JSON that describes the problem, follows behind. So from the status alone, the planner can tell whether the body is holidays or a description of a problem, before it reads any of it.

The rule is that the promise from fetch() fulfills as soon as the status line and headers arrive. The body may still be on its way, and response.json() waits for all of it.

The Final Code

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, you logged Portugal’s status, its ok flag and a header between the two awaits. Those three lines ran before the body had been read. So how can the status be known that early?

00:14 In this video, you’ll watch a response arrive on a timeline, and then time its two steps in a real run. Here’s the planner on the left, and Nager.Date on the right, with time running down.

00:29 The call to fetch hands back a promise right away, still pending. Then Node sends the GET request for Portugal’s holidays, and the first await waits on that promise.

00:40 The response comes back the way curl printed it, top first. The status line and headers arrive first. That’s all fetch waits for. Its promise fulfills with a Response, and the status, the ok flag and the headers are already in it.

00:56 So the first await is done, and your three log lines run.

01:02 In this picture, the body is still on its way behind them. On a fast connection, some of it may already be here, but the Response hasn’t read any of it yet.

01:12 Calling response.json() gives you a second promise. It reads the body to its end, whether it has already arrived or is still arriving, then parses it as JSON.

01:23 Only then does it fulfill, with Portugal’s 17 holidays, and the second await is done.

01:31 You can time the two steps with a clock. This script, arrive.mjs, sends the same request, and notes the time right before it. After each await, it logs how many milliseconds have passed.

01:46 Run it, and the status 200 is logged after about a tenth of a second. The body is read a few milliseconds later. These times show when your code resumes, not when the bytes arrive.

01:58 The timeline isn’t to scale: most of that tenth of a second is the request going out and the server answering. Your times will differ on every run, but the order stays the same.

02:10 Portugal’s list is small, so the second step is quick. A bigger body or a slower connection makes that step longer.

02:19 Now ask for XX, the made-up country code that got a 404 back when you sorted status codes. Change the country code in the URL, and run the script again. The status is 404, and it’s logged after the first await, the same as Portugal’s 200.

02:38 On the timeline, the 404’s status line arrives first too. Its body, the JSON that describes the problem, follows behind. So from the status alone, the planner can tell whether the body is holidays or a description of a problem, before it reads any of it.

02:56 Here’s the rule: the promise from fetch fulfills as soon as the status line and headers arrive. The body may still be on its way, and response.json() waits for all of it.

03:09 The first await is where you can check the status, before you touch the body. Next, in Check the Status Before the Data, you’ll make the planner check that status before it trusts the data.

Become a Member to join the conversation.