JS basis - the event queue

Why JavaScript is single-threaded?

A major feature of the JavaScript language is single-threaded, that is to say, the same time can only do one thing. So why not have JavaScript multiple threads do? This improves the efficiency of ah.

JavaScript is single-threaded, and its related purposes. As a browser scripting language, JavaScript main purpose is to interact with the user, as well as operating DOM. This determines that it can only be a single-threaded, otherwise it will bring a very complex synchronization problems. For example, suppose JavaScript while there are two threads, one thread to add content on a DOM node, another thread to delete this node, then the browser which thread should prevail?

So, to avoid complexity, from the birth, JavaScript is single-threaded, which has become a central feature of this language, will not change.

To take advantage of the computing power of multi-core CPU, HTML5 Web Worker proposed standard that allows JavaScript scripts to create multiple threads, but the main thread child thread completely under control, and shall not operate DOM. So, the new standard does not change the nature of single-threaded JavaScript.

Event Loop

Reference Address: Event Loop the loop you know what? (Detailed attached GIF) - hungry it distal end

The nature of the task queue

  • All synchronization tasks are performed on the main thread to form a execution stack (execution context stack).
  • Outside the main thread, there is a " task queue " (task queue). With asynchronous tasks run as long as a result, an event is placed in the "task queue" at home.
  • Once the "execution stack" all synchronization task is finished, the system will read the "task queue" to see what events there. Those corresponding asynchronous tasks, thus ending the wait state, into the execution stack, started.
  • The main thread repeat the third step above.

Asynchronous tasks

  • setTimeOut、setInterval
  • DOM events
  • Promise

Asynchronous JavaScript programming methods to achieve?

  • Callback
  • Event Listeners
  • Publish / Subscribe
  • Promises objects
  • Async function [ES7]

Comparing the setTimeOut, setImmediate, process.nextTick () of

setTimeout()

The events into the event queue, you must wait until the current code (execution stack) executed, the main thread will go to perform its specified callback function.
When the main thread execution time is too long, we can not guarantee that in the event callbacks will execute the specified time.
SetTimeout browser every time there is a delay of 4ms, when performing multiple consecutive setTimeout, there is likely to block the process, causing performance problems.

setImmediate()

Events into the event queue tail, executed immediately after the main thread of execution and function of the event queue is completed. And the effect of setTimeout (fn, 0) of the same.
The method provides the server node. The latest browser api had a similar realization: window.setImmediate, but rarely supported browser.

process.nextTick()

Inserted into the event queue tail, but will be executed before the next event queue. In other words, it specifies the task always take place before all asynchronous tasks, the end of the current main thread.
General process: - Before> next Event Loop (main thread reads "Task Queue") - current "execution stack" tail> trigger process specified callback function.
Way server node provides. This method can be used with asynchronous delay in question.
Can be understood as: This does not work, the next appointment takes precedence.

Browser Tasks, microtasks, queues and schedules

Promise

Promise itself is synchronized immediately execute the function, when executed resolve or reject the executor, in which case the operation is asynchronous, first performs then / catch the like, when the main stack is completed, to call the method will resolve / reject stored in execution, p print when printed results are returned, a Promise instance.

async await

Async / Await function is to generate a self-executing. Using the characteristic function to generate asynchronous code written in "sync" form.

async function returns a Promise object when the function is executed, the event will first await the return until the asynchronous operation triggered completed, then perform the back of the body of the function statement. It can be understood as, let out a thread out of the async function body.

Guess you like

Origin www.cnblogs.com/nayek/p/11729923.html