It's enough to talk about js promise. It's enough to talk about jspromise.
I. background
We all know that nodejs is very fast. Why is it so fast? The reason is that node uses asynchronous callback to process the events that need to be waited, so that the Code will continue to be executed without waiting somewhere. But there is also a bad thing. When we have a lot of callbacks, for example, after the callback is executed, we need to execute the next Callback and then execute the next Callback, this will cause layer-by-layer nesting, unclear code, and easy access to the "Callback Prison", which will easily result in the following example:
async(1, function(value){ async(value, function(value){ async(value, function(value){ async(value, function(value){ async(value, function(value){ async(value, final); }); }); }); });});
This writing method will cause people to crash. Is there any way to solve this problem, or is there any other way of writing? The answer is yes. es6's new promise object es7 async await can solve this problem. Of course, we will introduce the promise object first. es7's async await will be shared in later articles. The following describes the Promise object in the future.
II. Introduction
Promise is an object used to process asynchronous operations. It allows us to write asynchronous calls more elegantly and easily. As the name implies, Promise means that after Promise is used, he will certainly give us a reply, whether it is successful or failed, so we don't have to worry about him running. Therefore, Promise has three states: pending, resolved, and rejected ). Only the structure returned asynchronously can change its status. Therefore, there are generally two promise processes: pending-> resolved or pending-> rejected.
The promise object also has a common then method for executing the callback function. The then method accepts two parameters. The first is the successful resolved callback, and the other is the failed rejected callback, the second failed callback parameter is optional. In addition, the promise object can be returned in the then method, so that the call can be chained. The Code is as follows:
Var Pro = function (time) {// return a Promise object return new Promise (function (resolve, reject) {console. log ('20140901'); // call setTimeout (function () through the simulated interface {// The Promise is successful, then execute the first function resolve ('success failed') ;}, time) ;}; (function () {console. log ('start'); Pro (3000 ). then (function (data) {console. log (data); return Pro (5000 );}). then (function (data) {console. log (data); console. log ('end ');})})();
In the above Code, a Pro variable is defined, and an anonymous function is assigned to it. The function returns a Promise object and receives a function in the object, the resolve and reject methods are transmitted as parameters respectively, and setTimeOut is used to simulate asynchronous requests. After the resolve method is executed, a function of the then method is called. The result is as follows:
3. Promise APIs
1. Promise. resolve ()
2. Promise. reject ()
3. Promise. prototype. then ()
4. Promise. prototype. catch ()
5. Promise. all () // all are completed, which is equivalent
6. Promise. race () // you can complete one, which is equivalent to or
1. The role of Promise. resolve () converts an existing object to the Promise object resolvedl; Promise. resolve ('test') = new Promise (resolve => resolve ('test '))
2. Promise. reject () also returns a Promise object in the rejected state;
3. The then method has been described above and will not be introduced here.
4. catch (): callback function with an error.
5. Promise. all () is applicable to the successful then () operation after all results are completed. For example:
let p1 =new Promise(function(resolve,reject){ resolve(1); }); let p2 = new Promise(function(resolve,reject){ resolve(2); }); let p3 = new Promise(function(resolve,reject){ resolve(3); }); Promise.all([p1, p2, p3]).then(function (results) { console.log('success:'+results); }).catch(function(r){ console.log("error"); console.log(r); });
Final output:
6. Promise. race () is also used to execute multiple instances at the same time. If one instance changes the status, Promise changes to the status changed by that instance;
Iv. Example
Var Pro = function () {// return a Promise object return new Promise (function (resolve, reject) {// call the setTimeout (function () simulation interface () {resolve (true) ;}, 1000) ;}; var Pro2 = function () {// return a Promise object return new Promise (function (resolve, reject) {// simulate interface call setTimeout (function () {resolve ('successful execution of pro2') ;}, 1000) ;}; Pro (). then (function (data) {var val = data; console. log (val) if (val) {console. log (1111) return Pro2 ()}}). then (function (data1) {console. log (data1 )})
Output:
In this way, you can use the then method to implement chained calls.
The above discussion about js promise is enough to read all the content shared by the small editor. I hope to give you a reference and support for the customer's house.