Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
console.log(1);
setTimeout(() => {
console.log(2);
}, 0);
console.log(3);1 3 2
- Expecting
1 2 3: a delay of0does not mean "right now"; the callback waits until the current code has finished, so3is printed first. - Expecting the timer to be lost when the last line ends: Node keeps the program running until the waiting
setTimeoutcallback has run.
- What does the program print if the delay is
100instead of0? - What does the program print if a second
setTimeout(…, 0)printing4is added after the first?