Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
console.log(1);
setTimeout(() => {
console.log(2);
}, 0);
Promise.resolve().then(() => {
console.log(3);
});
console.log(4);1 4 3 2
- Expecting
1 4 2 3because the timer was set first: athencallback runs before anysetTimeoutcallback waiting at the same moment. - Expecting
3before4: even for a promise that is already fulfilled,thenonly schedules the callback, and it runs after the current code ends.
- What does the program print if the
setTimeoutdelay is10instead of0? - What does the program print if a second
.thenprinting5is chained after the first?