12.11. Predict the output of eight async snippets
Topics
This question is in
12. Callbacks, promises and async"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]toanswers[8], each an array of strings, for example1: ["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:
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");Snippet 2:
console.log("1");
new Promise((resolve) => {
console.log("2");
resolve();
console.log("3");
}).then(() => console.log("4"));
console.log("5");Snippet 3:
async function run() {
console.log("B");
await null;
console.log("D");
}
console.log("A");
run();
console.log("C");Snippet 4:
Promise.resolve().then(() => console.log("a1")).then(() => console.log("a2"));
Promise.resolve().then(() => console.log("b1")).then(() => console.log("b2"));Snippet 5:
setTimeout(() => console.log("t2"), 10);
setTimeout(() => {
console.log("t1");
Promise.resolve().then(() => console.log("m1"));
}, 0);
setTimeout(() => console.log("t3"), 0);Snippet 6:
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:
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:
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
answers[n]is an array of strings for every snippet from 1 to 8.- Its first lines are the lines snippet
nprints synchronously, during the run of the snippet itself, in order. - 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:
setTimeout(() => console.log("later"), 0);
console.log("now");has the answer:
["now", "later"]Environment
- Each snippet runs on its own, in a fresh page state, in a browser.
- The tests replace
setTimeoutwith 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
- For each snippet, write two lists as you read it: the microtask queue and the timer list. Which callbacks join each list, and when?
- A
thencallback is queued only when its promise settles. When does the promise returned by the firstthenof 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