"Javascript" JS synchronous asynchronous and callback functions

Source: Internet
Author: User
Tags hosting

First, preface

Today to see the knowledge of Requirejs, looking at the JS in the synchronous and asynchronous operation of the knowledge point, after consulting a lot of information, basic understanding of JS synchronous and asynchronous operation, which involves the following knowledge points:

    • What is synchronous and asynchronous?
    • JS is an event-driven single-threaded language, why there will be asynchronous operation of this multi-threaded operation???
    • Browser thread, browser kernel thread co-operation?
    • What are the asynchronous operations of JS? How does it work?

Second, JS single thread

    • JS's single thread

The meaning of single-threaded is that JS can only run on one thread, that is, the only thing that can be done at the same time, the other tasks will be placed in the queue queued and so on JS thread processing.

However, it is worth noting that although JS is a single-threaded language, it does not mean that there is only one JS engine thread in the browser kernel. JS engine has multiple threads, one main thread, and the other threads work with the main thread

Third, why JS Select Single thread

is concerned with its use. As a browser scripting language, JavaScript is primarily used to interact with users and manipulate the DOM. This determines that it can only be single-threaded, or it can lead to complex synchronization problems. For example, suppose a JavaScript colleague has two processes, one thread adds content to a DOM phase, and the other thread deletes the node, which thread should the browser take precedence over? If you want to implement this problem, you must add the concept of the thread lock, which is much more complicated.

Single-threaded and asynchronous can not be a language at the same time, JS choose to become a single-threaded language, so it is not possible to asynchronous, but JS hosting environment (such as browser, Node) is multi-threaded, the host in some way, so that JS has asynchronous properties, if you understand, You will find the JS mechanism is simple and efficient!

Iv. Synchronous and asynchronous

    • Synchronization: Only one task can be completed at a time, if there are multiple tasks, must be queued
    • Async: Each task has one or more callback functions (callback), after the previous task executes, the inside callback function is thrown, to the browser of another thread processing. The next task is to execute without waiting for the previous task to end.
    • An example to illustrate asynchronous and synchronous:

      

V. Benefits of Asynchrony

1. Benefits of Asynchrony

With a synchronous and an asynchronous diagram, it is assumed that there are four tasks (1, 2, 3, 4), each of which executes at 10ms, where Task 2 is the predecessor of Task 3 and Task 2 requires 20MS response time.

2. Suitable for the scene

It can be seen that when our program requires a lot of I/O operations and user requests, JS this has a single-threaded, asynchronous, event-driven multi-temperament of the language is how the occasional! It does not have to spend too much overhead and does not have to be used to handle multithreading, compared to the language of synchronous execution, where the asynchronous and event-driven mechanism of the hosting environment makes it non-blocking I/O, so you should know what kind of scene it fits into.

V. What are the asynchronous operations of JS?

    • Timer function settimeout (given to timer threading)
    • Event binding (onclick, onkeydown ....), given to event-triggered threading)
    • AJAX (handing over to HTTP thread processing)
    • callback function

We already know that JS has been a single-threaded execution, the browser for a few obvious time-consuming tasks (above four points), separate threads to solve time-consuming problems. But JS in addition to these obvious time-consuming problems, we may write our own program will also have time-consuming functions, how to solve? We are certainly not able to create separate threads directly, but we can use our browser to develop Windows ( timer threads and event-triggering threads )

Vi. Task Queue

Because JavaScript is designed as a single thread, it means that only one task can be performed at a time, and all tasks need to be queued

"Javascript" JS synchronous asynchronous and callback functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.