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

Ignore Yesterday's Late Reply

In this video, you watch a report from yesterday’s morning routine arrive after today’s and overwrite the Lindqvists’ kitchen status panel. In the last lesson, the problem was a second tap starting the routine twice. This time, the trouble is an old routine that finishes late.

The timings are a scaled-down toy: one second stands in for the night between the two mornings. Tuesday’s report takes five seconds, and Wednesday’s routine starts one second later and reports in two.

Key Points

  • Replies can come back in a different order from the one their work started in. Tuesday’s routine started first, but Wednesday’s report came back first, at three seconds, and the status showed it. Tuesday’s came back at five seconds and overwrote it, so the panel ended on yesterday’s report. Outside this toy, too, a newer request can start before an older one answers, and then the older answer can land last.
  • The code after an await runs only when its own reply arrives. At five seconds, Node’s event loop moved Tuesday’s timer task onto the call stack, and that task fulfilled its promise. Then a microtask resumed Tuesday’s call where it paused.
  • A late reply overwrites a newer one. Tuesday’s call wrote its reply into the status, with no idea that a newer routine had started. That’s a race condition, because the result depends on which reply happens to arrive last.
  • A run counter tells a late reply from the latest one. Each call adds one to latestRun and keeps its own number in run before it awaits anything, so Tuesday’s run stays 1 while latestRun moves on to 2. After the await, it compares the two. If they differ, a newer run has started, so the call ignores its reply and returns without touching the status.
  • Take a number before you wait, and check it after. Each run takes its number before the await, as the button claimed the morning in the last lesson. It checks the number after the await, because the world can change while you wait. Before you use a reply, check that it still belongs to the latest run.
  • Aborting is the other way out. You could also abort yesterday’s run with an AbortController, but only if its timer gets the signal and you catch the rejection, as in Make Your Own Functions Cancellable. The counter works even for work you can’t cancel.

The 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, Saga’s tap came while the routine was still running, and the fix was to claim the morning before waiting for anything. Her tap was turned away.

00:11 This time, nothing starts twice.

00:15 The Lindqvists have a status panel in the kitchen that shows the latest routine’s report. Yesterday, the blinds were offline, and the routine was slow to report back.

00:25 In this video, you’ll watch two mornings’ reports come back, see which one the panel ends on, and then make it show only today’s.

00:34 Here’s the program, a small standalone script. The status variable at the top stands in for the panel. The routineReport() function is a stand-in for a whole routine.

00:45 It waits, then resolves with that day’s report.

00:49 Each call to updatePanel() logs that it starts, and then awaits its report. When the report arrives, the call writes it into the status. At the bottom, Tuesday’s routine starts first, and its report takes five seconds.

01:04 One second later, Wednesday’s starts, and its report takes two. Here, one second is the night between the two mornings, so this is a scaled-down toy. The last line then awaits both of them with Promise.all().

01:21 Run it. Tuesday’s routine starts at zero and pauses to wait for its report. At one second, Wednesday’s starts and pauses too. Your turn: pause the video and predict what the status says once both reports are in.

01:41 It ends on yesterday’s report. Wednesday’s report came back first, at three seconds, and the status showed it. Then Tuesday’s came back at five seconds and overwrote it.

01:54 The older report won because each call’s code after its await runs only when its own report arrives. At five seconds, Tuesday’s timer task runs, and a microtask resumes Tuesday’s call where it paused.

02:08 It writes its reply into the status, with no idea that a newer routine has started.

02:14 The routines started in one order, and their reports came back in another. That’s a race condition again, because the result depends on which reply happens to arrive last.

02:25 This isn’t only a toy problem. Whenever a newer request starts before an older one answers, the older answer can land last.

02:34 To fix it, each run needs to know whether it’s still the latest when its report arrives. Add a counter at the top, latestRun, starting at zero.

02:45 Each call adds one to it and keeps its own number in a constant, run, before it awaits anything. So Tuesday’s call is run one, and Wednesday’s is run two.

02:57 Each call keeps its own run, so Tuesday’s stays one while the counter moves on. After the await, each call compares its own number with the counter.

03:06 If they differ, a newer run has started, so the call returns without touching the status. Run it again. At three seconds, Wednesday’s report sets the status.

03:19 At five seconds, Tuesday’s run is one but the counter is two, so it ignores its reply. The panel keeps today’s report.

03:30 The fix uses both halves. Each run takes its number before the await, as you claimed the morning in the last lesson. Then it checks it after the await, because the world can change while you wait.

03:42 So here’s the rule: before you use a reply, check that it still belongs to the latest run. You could also abort yesterday’s run with an AbortController, but only if its timer gets the signal and you catch the rejection.

03:57 The counter works even for work you can’t cancel.

04:01 Your panel now shows only the latest report, however late an older one turns up. Next, in Keep Updates From Overlapping, you’ll see two updates to one shared log get in each other’s way.

Become a Member to join the conversation.