preface
In the last article we talked about how event loops work in browsers. In this article we’ll talk about what happens when the host environment becomes Node.js.
A preliminary study
Let’s take a macro look at what an Event Loop looks like in Node.js.
┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ > │ timers │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ pending Callbacks │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ idle, Prepare │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ incoming: │ │ │ poll │ < ─ ─ ─ ─ ─ ┤ connections, │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ data, Etc. │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ check │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ └ ─ ─ ┤ close callbacks │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code
The six stages are described on the website as follows:
- Timers: This phase has been executed
setTimeout()
和setInterval()
The scheduling callback function of. - Pending callbacks: I/O callbacks that are deferred until the next iteration of the loop.
- Idle, prepare: used only in the system.
- Poll: Retrieves new I/O events; Perform I/ O-related callbacks (in almost all cases, except for closed callback functions, those made by timers and
setImmediate()
Beyond scheduling), the rest of the caseNode
Will block here in due course. - Check:
setImmediate()
This is where the callback function is executed. - Close callbacks: Some closed callback functions such as:
socket.on('close', ...)
.
setImmediate
This is an API implemented in Node.js but not in browsers. Let’s take a look at its definition:
This method is used to place long-running operations in a callback that is executed as soon as the browser completes the rest of the statement.
This feature is non-standard, so try not to use it in production!
SetImmediate Looks similar to setTimeout, but both run in different phases. SetImmediate performs a callback once it’s done in the current polling phase.
Let’s take a look at the code:
setTimeout(() = > {
console.log('setTimeout');
}, 0);
setImmediate(() = > {
console.log('setImmediate');
});
Copy the code
Do you think setTimeout or setImmediate?
In fact, we found after the implementation of the operation, it is possible!!
Here’s what the official said:
- The order in which timers are executed will vary depending on the context in which they are invoked.
- If both are called from within the main module, the timer is constrained by process performance (which may be affected by other running applications on the computer).
- If you call these two functions in an I/O loop,
setImmediate
Always limited calls.
To simplify this, it basically means that they have different priorities.
process.nextTick()
Process.nexttick () is an asynchronous task and is a microtask.
Process.nexttick () adds callback to the “Next Tick queue”. This queue is completely emptied after the current operation on the JavaScript stack has run, and before the event loop is allowed to continue. If process.nexttick () is called recursively, an infinite loop may be created.
summary
Finally, let’s list the micro and macro tasks in Node.js:
- Macrotask:
setTimeout
,setInterval
,setImmediate
,The overall code
,I/O
. - Common Microtasks:
process.nextTick
,Promise
And so on.
In Node.js, tasks are scheduled in six phases, which is slightly different from the browser. The sequence of setTimeout and setImmediate execution varies depending on the current context.
Finally, a small advertisement, meituan school recruitment club recruitment push, no restrictions on departments, no restrictions on posts, no restrictions on the number of delivery, massive HC, come quickly come ~