๐ฆ๐ฐ๐ต๐ฒ๐ฑ๐๐น๐ถ๐ป๐ด ๐ ๐ฒ๐ฐ๐ต๐ฎ๐ป๐ถ๐๐บ๐ ๐๐ป ๐ก๐ผ๐ฑ๐ฒ.๐ท๐ ๐๐ ๐ฒ๐ฐ๐๐๐ถ๐ผ๐ป You might think Node.js runs async code in the order it appears in your file. But it does not. Execution order depends on sync code, process.nextTick(), microtask queue, and event loop phases. When your code runs, different types of work go into different queues.
- nextTick()
- promise microtasks
- phase-based queues in the event loop Node.js runs all sync code first. This includes things like console.log() or normal function calls. After that, it deals with queued work. process.nextTick() runs first because it has higher priority. Then microtasks (mainly Promises) are executed. Once both are cleared, the event loop moves into its phases. Between these phases, Node.js checks nextTick() and microtasks again. Source: https://dev.to/neel-vekariya/scheduling-mechanisms-in-nodejs-execution-5gb0