NodeJS event loop

1. DESCRIPTION execution sequence

1. Empty the main execution stack

2. Clear the micro-tasks queue

3. Run a timer queue callback function, ask micro task queue, if there is a callback function cleared.

4. Step 3 cycles, until the timer queue empty

The queue enter poll, callback function to poll the queue, the task queue asking micro, micro task queue to empty.

6. Step 5 cycles, the queue empty poll

7. Empty check queue

8. The loop from step 2 again

 Example 1:

setTimeout(() => {
    console.log('timeout')
})
setImmediate(() => {
    console.log('immediate')
})

// After node command is executed, depending on the operating environment, not necessarily the order. 
// If setTimout callback queue to enter, execute; otherwise setImmediate first execution

 

Guess you like

Origin www.cnblogs.com/lyraLee/p/11867953.html