JavaScript · Junior · Question 32 of 75

In what order are the numbers printed?

console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
console.log(4);

Explanation

Synchronous code runs first (1, 4). Then the event loop runs all microtasks - promise callbacks (3) - and only then the next macrotask - the timer (2). setTimeout(fn, 0) doesn't mean "immediately".

Further reading

Problem with a question

JavaScript · Junior · question 32

Test yourself

Take the JavaScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz