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

See Where Callbacks Break Down

In this lesson, you grew the laundry-duty program from the last lesson by one more step, and then handed a callback to a helper that didn’t behave. Both show where callbacks start to break down as a program grows.

Key Points

  • Each callback you put inside another is one level deeper. Each result arrives as a callback parameter, which only exists inside its own callback. Here, nesting is how the program keeps all three results in reach, so each step that needs an earlier result goes inside that step’s callback.
  • With nested callbacks, every step that waits pushes everything after it one level to the right. Each level repeats the same error check, and the closing brackets pile up at the end, where you have to match each one to its step. Developers call this shape callback hell.
  • When you pass a callback to readFile(), Node calls it once, and always later. With someone else’s code, you trust what it says it does. Kofi says his readNote() calls back once, and always later, too.
  • Once you hand your callback over, the other code decides when it runs, how often, and whether it runs at all. Usually, your code calls functions and stays in charge. Here, Kofi’s code calls yours. Developers call this inversion of control. A helper that breaks its once-and-later rule can call your callback twice, never, or right away:
  • Twice: your callback’s work happens twice. If your callback also passed duty on to the next housemate, duty would pass twice and skip Kofi.
  • Never: there’s no result and no error. The program ends in silence.
  • Right away: readNote() calls your callback itself, on the same stack, before it returns, so the callback runs before the line after the call. It never goes near the task queue. Not every callback waits. The line after the call then announces a lookup that’s already over: code after the call can’t count on the callback coming later.
  • Those two problems, nested callbacks and inversion of control, are why JavaScript developers came up with promises. That’s for the next part.

A Helper That Does Both

The video mentioned a helper that waits one time and answers right away the next. Here’s one. This version of Kofi’s readNote() keeps each note it has read in a Map, so it doesn’t have to read the file again:

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 read the house rota, then the note of whoever’s on duty. Here’s that program. The second read sits inside the first callback, because it needs the name the first one found.

00:12 Run it, and it prints Ines’s note. Each callback you put inside another is one level deeper. So the line that prints Ines’s note is 2 levels deep. In this video, you’ll see where callbacks break down as a program grows.

00:29 Ines’s duty includes cleaning the dryer’s lint filter, so the report should also count the loads since the last clean. That count comes from a third file, the dryer’s log, with one line per load.

00:43 The report needs all three results together. Each result is a callback parameter, which only exists inside its own callback. Here, nesting is how the program keeps all three in reach, so the third read goes inside the second callback, one level deeper.

01:00 Then it counts the non-empty lines and logs the two report lines. Run it, and the report has both lines. The program works, but the lines that print the report are now 3 levels deep.

01:15 Step back and look at the shape. With nested callbacks, every step that waits pushes everything after it one level to the right. Developers call this shape callback hell.

01:28 Each level repeats the same error check. The closing brackets pile up at the end, and you match each one to its step. Nesting is the problem you can see. The next one hides inside every callback you hand over.

01:43 When you pass a callback to readFile(), Node calls it once, and always later.

01:50 With someone else’s code, you trust what it says it does. Say Kofi writes a helper module with a readNote() function. It’s a toy. It waits a tenth of a second, then answers with Ines’s note, whoever you ask about.

02:03 Kofi says it calls back once, and always later. Your program asks it for Ines’s note and logs the note when it arrives. Its last line logs that the lookup has started.

02:15 Run it. The looking up line prints first, and the note comes later. But Kofi’s code now decides when your callback runs, how often, and whether it runs at all.

02:26 Usually, your code calls functions and stays in charge. Here, Kofi’s code calls yours. Developers call this inversion of control.

02:37 From here on, your program stays the same. Only Kofi’s helper changes, and each version breaks that once and later rule. This one answers, then answers again a moment later.

02:49 Ines’s note prints twice. If your callback also passed duty on to the next housemate, duty would pass twice and skip Kofi. This version only answers when you ask about Dev.

03:03 Ask about Ines, and nothing ever calls your callback. There’s no note and no error. The program ends in silence.

03:13 The last version doesn’t wait. Inside readNote(), it calls your callback straight away. Which line prints first now? Your turn: pause the video and predict the first line.

03:30 Ines’s note. readNote() calls your callback itself, on the same stack, before it returns. It never goes through the task queue. Not every callback waits.

03:41 This one ran now. So the line after the call announces a lookup that’s over. A helper can even wait one time and answer right away the next, as the text below shows.

03:54 Look back at the two programs. Nested callbacks put each dependent step one level deeper. And each callback you hand over runs when, how often, and whether the other code decides.

04:08 Those two problems are why JavaScript developers came up with promises. That’s for the next part. Next, in Exercise: Check Who’s Next on the Rota, you’ll find who’s next on the rota and read their note, one step after the other.

Become a Member to join the conversation.