$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #109

Which fix removes the race?

let seq = 0;
async function next() {
  const v = seq;
  await tick();
  seq = v + 1;
  return seq;
}
  1. Increment before awaiting: seq += 1 first
  2. Increment before awaiting: seq == 1 first
  3. Wrap the body in a try/finally block
  4. Change let seq to const seq at the top
Two concurrent calls both read the old value across the await and write the same number.
Play Problem Solving ▸