Skip to curriculum
Jump to the editor

12.11. Predict the output of eight async snippets

Medium
Topics
20–30 min

"What does this print?" is the quickest way to check whether someone really knows how JavaScript runs asynchronous code. The answer always comes from the same few rules: synchronous code runs to the end, then the microtask queue (promise callbacks, queueMicrotask, the rest of an async function after await) is emptied, and only then does the next task, such as a timer callback, run. Work out each snippet below with those rules before you run anything.

In answers.js, fill the answers object: for each snippet, the lines it prints, in order, as strings.

Files

  • answers.js: answers[1] to answers[8], each an array of strings, for example 1: ["A", "B"].
  • The tests run each snippet, record what it really prints, and compare. They check the synchronous lines first, then the whole order.

The snippets

Snippet 1:

JAVASCRIPT
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");

Snippet 2:

JAVASCRIPT
console.log("1");
new Promise((resolve) => {
  console.log("2");
  resolve();
  console.log("3");
}).then(() => console.log("4"));
console.log("5");

Snippet 3:

JAVASCRIPT
async function run() {
  console.log("B");
  await null;
  console.log("D");
}
console.log("A");
run();
console.log("C");

Snippet 4:

JAVASCRIPT
Promise.resolve().then(() => console.log("a1")).then(() => console.log("a2"));
Promise.resolve().then(() => console.log("b1")).then(() => console.log("b2"));

Snippet 5:

JAVASCRIPT
setTimeout(() => console.log("t2"), 10);
setTimeout(() => {
  console.log("t1");
  Promise.resolve().then(() => console.log("m1"));
}, 0);
setTimeout(() => console.log("t3"), 0);

Snippet 6:

JAVASCRIPT
setTimeout(() => console.log("timeout"), 0);
Promise.resolve()
  .then(() => {
    console.log("then 1");
    setTimeout(() => console.log("inner timeout"), 0);
  })
  .then(() => console.log("then 2"));
queueMicrotask(() => console.log("microtask"));
console.log("sync");

Snippet 7:

JAVASCRIPT
async function fail() {
  console.log("start");
  throw new Error("boom");
}
fail().catch((error) => console.log("caught " + error.message));
console.log("after");
Promise.reject(new Error("no"))
  .finally(() => console.log("finally"))
  .catch(() => console.log("catch"));

Snippet 8:

JAVASCRIPT
async function one() {
  console.log("one start");
  await two();
  console.log("one end");
}
async function two() {
  console.log("two");
}
one();
Promise.resolve().then(() => console.log("then"));
console.log("sync end");

Requirements

  1. answers[n] is an array of strings for every snippet from 1 to 8.
  2. Its first lines are the lines snippet n prints synchronously, during the run of the snippet itself, in order.
  3. The whole array is exactly the snippet's output, in order: no line missing, none extra.

Examples

This snippet, which is not one of the eight:

JAVASCRIPT
setTimeout(() => console.log("later"), 0);
console.log("now");

has the answer:

JAVASCRIPT
["now", "later"]

Environment

  • Each snippet runs on its own, in a fresh page state, in a browser.
  • The tests replace setTimeout with a virtual clock that keeps the browser's order: timers by due time, then by creation, and all microtasks after each timer callback.
  • When an answer is wrong, the message says which line differs, not what it should be.

Hints

  1. For each snippet, write two lists as you read it: the microtask queue and the timer list. Which callbacks join each list, and when?
  2. A then callback is queued only when its promise settles. When does the promise returned by the first then of a chain settle?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…