This is the 12th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

If the number is less than or equal to 5, we consider the Promise “successful” and call resolve to change the state of the Promise. Otherwise we consider it a “failure” and call Reject with an argument as the reason for the failure.

Run getNumber and pass two arguments in the THEN. The THEN method accepts two arguments, the first for the resolve callback and the second for the reject callback. So we were able to get their data separately. Run this code several times, and you’ll get one of two random results:

or

We know that a Promise object has a catch method in addition to the THEN method. What does it do? This is the same as the second argument to then, which specifies the reject callback.

getNumber()
.then(function(data){
    console.log('resolved');
    console.log(data);
})
.catch(function(reason){
    console.log('rejected');
    console.log(reason);
});
Copy the code

The effect is the same as in the second argument of the then. If an exception is thrown during the resolve callback (the first argument in the then), it will not be reported that the js is stuck. Instead, it will be added to the catch method. Look at the following code:

getNumber() .then(function(data){ console.log('resolved'); console.log(data); console.log(somedata); // somedata is undefined}).catch(function(reason){console.log('rejected'); console.log(reason); });Copy the code

In the resolve callback, we console.log(somedata); The variable someData is not defined. If we don’t use the Promise, the code that runs here is going to get an error in the console, so it’s not going to run. But here, you get something like this:

That is, it goes into the catch method and passes the error reason to the reason argument. Even the code with an error will no longer report an error, which has the same function as our try/catch statements.

Promise’s all method provides the ability to perform asynchronous operations in parallel and to execute the callback only after all asynchronous operations have been completed. We still use the runAsync1, runAsync2, and runAsync3 functions defined above. Here’s an example:

Promise
.all([runAsync1(), runAsync2(), runAsync3()])
.then(function(results){
    console.log(results);
});
Copy the code

This is executed with promise. all, which takes an array of parameters whose values eventually return Promise objects. In this way, three asynchronous operations that are executed in parallel will not enter the THEN until they have all been executed. So where is the data returned by the three asynchronous operations? It’s all in the THEN, so all is going to put the results of all the asynchronous operations into an array and pass it to the THEN, which is the results up here. So the output of the above code is:

With ALL, you can perform multiple asynchronous operations in parallel and process all the returned data in a single callback. Isn’t that cool? There is a scene is very suitable for using this, some game class material more applications, open the web page, pre-loading the need to use a variety of resources such as pictures, Flash and a variety of static files. After everything is loaded, we initialize the page.