js threaded synchronous asynchronous request ajax

We often say that JS is single-threaded, single-threaded and that in the end is what? What is sync? What is asynchronous? How to solve the callback hell ??

  1. js blocking characteristics:
    JS has a very silent blocking characteristic is that when a browser in the implementation of JS code, can not do anything else at the same time, regardless of the code is embedded or external
  2. Synchronization: You do one thing, you can not do another thing at the same time.
  3. Asynchronous: You do one thing, it might be time-consuming for a long time, but this time you can wait for the process, do another thing.

For example, boil water at this thing ... In this process, you do not worry about the water boiling other things, wait until the water boils, it is synchronized.
And you think this process takes quite a long time, you can do other things, such as to sweep the floor, until the water boils.

  1. Single thread: the task can only be executed one by one, can not perform multiple tasks simultaneously, that is, no concurrency. js is single threaded

You might wonder, like me, everyone says that JS is single-threaded, asynchronous code, but why it has to say.

JS really is single-threaded. But it runs the browser kernel environment is multithreaded. Including the JS engine thread, eventing thread, http request thread, GUI rendering thread. But between threads are mutually exclusive. For example, when the JS execution, GUI rendering thread is in a suspended state.
I can not change the DOM node when you render the side of it, that will lead to significant performance loss, so it is witty ~

Usually said timer setTimeout, setInterval and triggered event onclick, onfocus, etc. and are asynchronous http requests.

JS engine thread will first perform synchronization code before you execute asynchronous code in a task queue inside.

Example:

This code output is 55555? ? ?

 <script>
        
        for (var i = 0; i < 5; i++) {
            function fun() {
                setTimeout(() => {
                    console.log(i);
                }, i * 1000);
            }
            fun();
        }
    </script>     

setTimeout(code,millisec)

code is necessary. JavaScript code string after the function to be called to perform.
millisec necessary Number of milliseconds to wait before the code is executed

Because the queue for synchronization loop execution is complete before the turn of the asynchronous queue,

The time for each cycle, setTimeout executed once, but the inside of the function (closure function) is not executed, but is placed inside the task queue, waiting to be executed,

When i is accumulated out of the cycle time to 5.

At this time, only a global variable i => 5, 5 are so printed.

https://www.jianshu.com/p/a81a6ed3ef3a
look at tomorrow

Constituent elements of asynchronous procedures

In fact asynchronous function call is completed soon, but after that there are worker threads to perform asynchronous tasks, notify the main thread, the main thread calls the callback functions, and so many steps,

We put the whole process is called asynchronous process, asynchronous function calls in an asynchronous throughout the process, only a small part;

An asynchronous process is usually like this:

  1. The main thread to initiate an asynchronous request, the corresponding worker thread receives the request and inform the main thread has been received (asynchronous function returns);

  2. The main thread can continue to execute the code behind, while the worker thread to perform asynchronous tasks;

  3. After worker threads to complete the work, notify the main thread;

  4. After the main thread notified perform certain actions (callback function);

Asynchronous call in two stages, and submit the request processing result, an invocation of the event loop between the two phases, they belong to two different event loop (the tick), not associated with each other,

Asynchronous call to the incoming callback general way to specify the action to be performed after the asynchronous operation is complete, and the body of an asynchronous call and callback events belong to different cycles;

https://blog.csdn.net/suoz_wly0710/article/details/77986530

Tomorrow continue
https://www.jianshu.com/p/a81a6ed3ef3a

Guess you like

Origin blog.csdn.net/weixin_44769592/article/details/91634097