Node.js · Middle · Question 3 of 75

Inside an I/O callback (for example, in fs.readFile), which runs first?

fs.readFile(__filename, () => {
  setTimeout(() => console.log("timeout"), 0);
  setImmediate(() => console.log("immediate"));
});

Explanation

After the poll phase the loop goes to the check phase, where setImmediate runs, before it comes back to timers. In the main module (outside I/O) the order of the two is not guaranteed.

Further reading

Problem with a question

Node.js · Middle · question 3

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