Explore Javascript asynchronous programming (1)

Source: Internet
Author: User

Explore Javascript asynchronous programming (1)

The problems caused by asynchronous programming are not obvious in the client Javascript. However, as the server Javascript becomes more and more widely used, a large number of asynchronous IO operations make the problem obvious. Many different methods can solve this problem. This article discusses some methods, but they are not in-depth. You need to select a method that suits your needs.

In my previous blog, I briefly discussed the similarities and differences between Python and Javascript. In fact, asynchronous programming as a programming language Javascript is a very interesting topic worth discussing.

Introduction to JavaScript asynchronous programming

Callback Function and asynchronous execution

The so-called Asynchronization refers to the asynchronous execution of callback functions instead of directly returning execution results.

Let's first look at what the callback function is:

 
 
  1. var fn = function(callback) { 
  2.     // do something here 
  3.     ... 
  4.     callback.apply(this, para); 
  5. }; 
  6.    
  7. var mycallback = function(parameter) { 
  8.     // do someting in customer callback 
  9. }; 
  10.    
  11. // call the fn with callback as parameter 
  12. fn(mycallback); 

A callback function is actually a function provided by the user. It is usually provided in the form of parameters. Callback functions are not necessarily executed asynchronously. In the preceding example, the callback function is executed synchronously. Callback is supported in most languages. C ++ can use function pointers or callback objects. Java generally uses callback objects.

In Javascript, there are many asynchronous calls executed by callback functions, such as setTimeout) or setInterval ).

 
 
  1. setTimeout(function(){ 
  2.     console.log("this will be exectued after 1 second!"); 
  3. },1000); 

In the preceding example, setTimeout is returned directly. An anonymous function is asynchronously triggered and executed after 1000 milliseconds, which may not be guaranteed to be 1000 milliseconds. This completes the operations on the print console. That is to say, in the context of asynchronous operations, the function returns directly and gives control to the callback function. The callback function will be scheduled for execution in a later time segment. So why does it need to be asynchronous? Why cannot I directly complete the operation in the current number of letters? This requires understanding of the Javascript thread model.


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.