is Multiple setTimeout execution single threaded or mutli threaded in Node.js?

  javascript, multithreading, node.js, settimeout

Suppose I have 3 setTimeouts like below:

console.log(new Date().toLocaleTimeString());

setTimeout(() => { console.log(new Date().toLocaleTimeString()); }, 3000);

setTimeout(() => { console.log(new Date().toLocaleTimeString()); }, 6000);

setTimeout(() => { console.log(new Date().toLocaleTimeString()); }, 9000);

Since Node is single threaded, execution of each timeout should be one after the other. Hence the output should be as expected as below:

9 am 0s (intial)

9 am 3s(+3s)

9 am 9s(+6s)

9 am 18s(+9s)

But what I found was:

9 am 0s (initial)

9 am 3s(+3s)

9 am 6s(+3s)

9 am 9s(+3s)

So it was clear that all the timeouts have been started at the same time.. difference between each timeout was added to each which in this case was +3s,

Does this make nodejs multi-threaded in this case? I do know that not all parts of node run within the same thread and few do get executed outside the same thread.

Can someone provide me a better sense in this. Or maybe my approach is wrong.

Source: Ask Javascript Questions

LEAVE A COMMENT