JavaScript Asynchronous Programming

Source: Internet
Author: User

JavaScript Asynchronous Programming

Chapter one in-depth understanding of JavaScript events

1. Javascirpt is generally single-threaded, SetTimeout and setinterval

Executes only when the CPU is idle.

var start = new Date; SetTimeout (function () {

var end = new Date;

Console.log (' Time elapsed: ', End-start, ' Ms '); }, 500);

while (new Date-start < 1000) {};

Explanation: SetTimeout, there is a delay event queued to handle the event. FIFO first. As with the binding mechanism of an event, the first binding on a DOM element is performed first.

2. Asynchronous type, mainly divided into I/O Asynchronous and timed asynchronous

I/O async is primarily a XMLHttpRequest request. Timing asynchronous is settimeout setinterval and so on. Add sync and Async to the function name in Nodejs to differentiate whether it is asynchronous.

3. Asynchronous function writing and asynchronous exception handling, TODO Future Analysis

4. The first stage mainly uses the callback function

Chapter II Distributed Events

1. Distributed events mainly utilize the PubSub mode. Specifically, node's Eventemitter object, backbone's event model, and jquery's custom events.

Principle: I wrote a custom event for jquery, and the basic principle is an event source, which simultaneously allows multiple places to respond synchronously. jquery version 1.7+ after priority use on

2. The call and apply mechanisms also need to study TODO

Chapter III Promise objects and deferred objects

1. Promise and deferred decouple the complex coupling structure, which is simple in structure and easy to expand. (JQuery 1.5+ Support)

var promise = $.get ('/mydata '); Promise.done (onsuccess);   Promise.fail (onfailure); Promise.always (onalways);

2. Python's twisted frame = "Dojo." The deferred object presents the PROMISES/A specification

3.

Always supported in the jquery 1.6+

var promptdeferred = new $.      Deferred (); Promptdeferred.always (function () {

Console.log (' A choice was made: '); });

Promptdeferred.done (function () {Console.log (' starting game ... ');});

Promptdeferred.fail (function () {Console.log (' No game Today ');});

Asynchronous operations generate promise objects, and withdrawals can define deferred objects for asynchronous result processing.

$ (' #playGame '). focus (). On (' KeyPress ', function (e) {

var Y = 121, N = 110;

if (E.keycode = = = Y) {

Promptdeferred.resolve ();

} else if (E.keycode = = = N) {

Promptdeferred.reject (); } else {

return false; The Deferred object here remains in a suspended state};

});

4. $.when

5. Promise.pipe

6. jquery's Promise and Commonjs promises/a are basically the same, Q.js library is the most popular promises/a implementation. jQueyr1.8 then to replace the pipe.

Over the past few years, promise is one of the most exciting new features of jquery. The more JavaScript APIs that return promise objects in the future, the more appealing these APIs will be.

Promise I contact very little, in the future in the actual work also need to further analysis and research, and constantly improve the ability of asynchronous processing in JavaScript.

The fourth Chapter Async.js workflow Control

Async.js is primarily used on the server side of node. js to write asynchronous functions in a write-synchronous manner.

The main functions provided are:

1. Async.filter

2. Async.foreach

3. Async.filterseries and Async.foreachseries

4. Reference API Design

5.async.series

6.async.parallel

7.async.queue similar to how banks work in multiple windows

Async.js's alternative is step.js, which is relatively concise and can be customized with various concurrency functions.

Async.js has become one of the leading workflow control libraries, providing robust iterative data collection methods and reliable scheduling tasks. If a workflow control problem is encountered, Async.js is likely to have a solution. If you prefer to solve it yourself, you might want to use Step.

The fifth chapter multithreading technology of Worker object

1. Worker allows multiple threads to be enabled at the same time

Sixth. Asynchronous script loading

1. Mainly introduces the location of <script> label

2. Async/defer support for asynchronous loading in HTML5

Defer will be executed after the HTML parsing is complete, window.load after the domcontentloaded.

Async means that scripts can be executed in a non-sequential order if the script satisfies the criteria.

3. Yepnope and Request.js support for asynchronous loading

JavaScript Asynchronous Programming

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.