Promise to perform multiple AJAX requests

Source: Internet
Author: User

Written in front: as an entry-level rookie, when the project cut the environment with NODE,VUE,ES6, found to learn to understand the things there are too many, before even promise is a nothing to know.

Turned over the book said promise is abstract asynchronous processing of objects and its various operations of the component, a little abstraction to understand is not AH ~ So consulted the great God

Simply put: Each asynchronous request immediately returns a Promise object, and because it returns immediately, it can take the process of synchronous operation. The promise then method allows you to specify a callback function that is called after the asynchronous task completes.

The following settimeout () can instead be interpreted as an AJAX request:

    

function A () {

return new Promise (function (resolve, reject) {

SetTimeout (function () {

Console.log (' Perform task A ');

Resolve (' Perform mission a success ');

}, 1000);

});

}

Function B () {

return new Promise (function (resolve, reject) {

SetTimeout (function () {

Console.log (' Perform task B ');

Resolve (' Perform task B success ');

}, 2000);

});

}

Function C () {

Console.log (' last execution C ')

}

B is executed only after a has been successfully executed, and then C (who requests execution) after B is executed successfully

A (). Then (b ()) and then (C ());

Promise to perform multiple AJAX requests

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.