I am new to NodeJS and I am trying to get my head around how the event loop works in various scenarios.
Take the following code snippet :
console.log("Hello");
setImmediate(() => {
console.log("Immediate");
});
console.log("World");
It gives the following output:
Hello
World
Immediate
It only makes sense if you are willing to believe that all async calls happen after all sync calls.
Is that true ? Are all node sync calls suppose to execute before the async calls.
When does the event loop event start ? After parsing the whole file or when the node runtime starts up ? Or when it encounters the first async call ?
I can rationalize the above output only if I accept one of the presumptions, either all async calls happen after sync calls, ie. event loop starts after all sync calls are completed, or setImmediate()
here takes more time to complete than console.log()
Which one is correct ?