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