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

await Pauses the Function, Not the Program

In this lesson, you stepped into Node’s REPL with a log before, inside and after a call to an async function, and watched the caller carry on while the function waited.

Key Points

  • A common guess is that await stops everything until the value arrives, the way code waits when it runs top to bottom. It doesn’t.
  • wait() from Wrap a Callback API in a Promise gives back a promise that’s fulfilled after the number of milliseconds you pass. Here, wait(300) stands in for the kiosk’s slow answer.
  • A line that logs welcome, calls load() and then logs menu shown prints welcome, loading, then menu shown. The REPL’s prompt comes back while load() is still waiting, and loaded prints last, 300 milliseconds later.
  • Your line runs on the call stack, logs welcome, and calls load(), which logs loading. On the await line, wait(300) runs first. Its timer goes to the waiting area, and it gives back a pending promise. Then load() steps aside. It leaves the stack and hands its own promise back to your line, still pending. Your line carries on and logs menu shown.
  • When the timer is due, it joins the task queue, and the event loop moves it onto the empty stack. Its callback fulfills the promise from wait(), so load() picks up right after its await, logs loaded, and its own promise is fulfilled. How load() gets back onto the stack in detail comes later in the course.
  • Carrying on is useful, because the rest of your program keeps going while the kiosk is slow.
  • When the next line needs the result, the caller awaits the promise load() hands back, as your loop over Tolu’s picks did with planPick(). With await load();, the order is welcome, loading, loaded, then menu shown. At the top level of a module or the REPL, await pauses the rest of the module or line, not Node’s event loop, which still runs the timer.

The rule is that await pauses the function it’s in, not the program. Only code after the await, in the same function, waits.

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, planPick waited for the kiosk at each await. So what does the rest of your program do while a function waits? A common guess is that everything stops until the value arrives, the way code waits when it runs top to bottom.

00:17 In this video, you’ll test that guess in Node’s REPL, with a log before, inside and after a call to an async function.

00:26 First, paste in the wait function from Wrap a Callback API in a Promise. It gives back a promise that’s fulfilled after the number of milliseconds you pass.

00:36 Here, 300 stands in for the kiosk’s slow answer.

00:42 Next, an async function called load. It logs loading first. Then it awaits a 300 millisecond wait, and logs loaded once that’s over.

00:53 Say the planning script will greet the family, load their picks, and then show them a menu. One line logs welcome before the call, calls load, and logs menu shown after it.

01:05 Your turn: pause the video and predict the order the four lines print in.

01:15 In this run, welcome, loading and menu shown printed, in that order. The undefined is the REPL’s echo of the line’s value, as for any line. Then the prompt came back, ready for your next line, while load was still waiting.

01:32 After 300 milliseconds, loaded printed last, on the prompt’s line.

01:38 If you guessed loaded before menu shown, that’s the guess from the start of this video. It’s the order you’d get if await stopped the whole program. It doesn’t, and here’s why.

01:51 Here’s your line on the left, and load on the right. Your line runs on the call stack, and logs welcome. It calls load, which goes on top, and logs loading straight away.

02:03 On the await line, wait(300) runs first. Its timer goes to the waiting area, and it hands back a pending promise. Then load steps aside. It leaves the stack, and hands its own promise back to your line, still pending.

02:19 Your line carries on, and logs menu shown right away. It finishes, the stack is empty, and the REPL shows its prompt. After 300 milliseconds, the timer is due, and joins the task queue.

02:33 The event loop moves it onto the empty stack, and its callback fulfills wait’s promise. So load picks up right after its await. Then loaded prints, load finishes, and its own promise is fulfilled.

02:48 How load gets back onto the stack in detail comes later in the course.

02:54 The rule is that await pauses the function it’s in, not the program. Only the code after the await, in that same function, has to wait. That’s useful, because the rest of your program can keep going while the kiosk is slow.

03:09 But the family’s menu should come after their picks have loaded. For that, your line awaits the promise load hands back. Put await in front of the call to load, and the order changes.

03:21 This time, welcome, loading and loaded print, and menu shown comes last.

03:28 The REPL lets you await at the top level, and there only the rest of your line waits.

03:35 Next, in Exercise: Plan the Family’s Museum Visit, you’ll plan the family’s picks from the kiosk’s answers.

Become a Member to join the conversation.