Where is the time between the execution of the js asynchronous task and the call back function?

recently, I have been studying event loop . I have read a lot of articles written by my predecessors, who said that when the main thread executes an asynchronous task, it initiates a call, then suspends it, and when it has a return value, it is placed in the task queue to wait for the main thread to call the callback function to get the return value.
but I don"t understand how to hang it. When the main thread performs another task when it is suspended, what is the suspended asynchronous task doing? Where?
such as ajax request, don"t you need a thread to run it when receiving data from the server?
is there anything my brother knows? Solve.

Mar.14,2021

    When the
  1. main thread executes the asynchronous call, it hangs the asynchronous code on the asynchronous thread provided by the browser kernel to execute, while the main thread continues to execute the Synchronize code.
  2. the callback is placed in the message queue after the asynchronous thread finishes execution.
  3. the main thread pass takes an asynchronous callback from the message queue through an event loop and then executes it.

such as an ajax request.
when the js engine executes the ajax request, the browser opens a new thread to execute the XMLHttpRequest, while the main thread provided by the js engine loops through the message queue to find the callback to execute.

answer the questions based on personal understanding. If there are any mistakes, please point them out directly and do not hesitate to give us advice.


this should involve the concept of an "event-triggered thread".
it is recommended to read from browser multi-process to JS single thread, the most comprehensive comb of JS running mechanism
may solve your doubts.

Menu