Skip to content
Onsite

Predict the order of setTimeout 0 and setImmediate in the main module versus inside an fs callback.

MediumP1Output

This Node.js code schedules a setTimeout(..., 0) and a setImmediate from inside an I/O callback. What does it print, and how would the order differ if both were scheduled in the main module?

1const fs = require('fs');
2fs.stat('.', () => {
3 setTimeout(() => console.log('timeout'), 0);
4 setImmediate(() => console.log('immediate'));
5});
Saved automatically

One line per console.log. Arrays as [1, 2, 3], objects as { a: 1 }, an uncaught error as Uncaught TypeError: message (Chrome and Node wording).

Hints

Saved automatically