1. Like the question, I want to ask a question about the js mechanism. Is it possible to wait for the main thread to finish executing before performing asynchronous operations?
2. Detailed explanation of JavaScript running mechanism: Let’s talk about Event Loop again. Most people have read this article. What Ruan Laoshi said is: Only after the main thread completes execution, the task queue will be read.
3. Today I heard the term context, which probably means that an asynchronous operation only waits for the main thread in the same context to complete its execution. without waiting for other context. I've been searching for a long time today, but I haven't found any similar articles. Please give me an answer! !
You confused a lot of things, as well as Ruan Yifeng’s article. . . Criticism is reading. . His article. . There are still many misconceptions. .
The following is an annotation by Pu Ling: you can take a look for reference
Pu Ling annotated version
nodeJS official documentation evenloop process.nextTick timers relationship
My understanding is that the main thread of node runs thread code. Only file I/O, network I/O, and timer are asynchronous events and will enter the asynchronous queue. Usually the code you write is synchronous.
For example, if you have Here’s a program
As for the concept of context, I don’t understand it thoroughly. I don’t know which one should be included. I hope someone can give me some advice. . Welcome to criticize or criticize, please tell me where I am wrong. . Hey hey