Node.js · Middle · Question 2 of 75

What is the order of the output?

setTimeout(() => console.log("timeout"), 0);
Promise.resolve().then(() => console.log("promise"));
process.nextTick(() => console.log("nextTick"));
console.log("sync");

Explanation

Synchronous code runs first. Then Node.js drains the process.nextTick queue, then the promise microtask queue, and only then moves to the event loop phases with timers.

Further reading

Problem with a question

Node.js · Middle · question 2

Test yourself

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

Take this quiz