the meaning of the Promise object:
To say a simple example, in the daily development, after a successful asynchronous request, get the requested parameters, and then the n operation down, and sometimes may also be able to use the asynchronous request followed by an asynchronous request, the following code is the most direct no brain writing:
$.ajax ({//NOTE: This is the first Ajax request
URL: ' ... ',
success:function (data) {
$.ajax ({//This is the second AJAX request
// The next URL can be executed after the first request succeeds
: ' ... ',
success:function (data) {
//...}}}
);
First of all, from the point of view of the code, this layer of nesting, really not elegant. Second, such an operation would result in a second request that might not be possible at all.
It's time for the Promise object to shine. What is a Promise object
That's what Nanyi's es6.
The so-called promise is simply a container that holds the result of an event (usually an asynchronous operation) that will end in a future. Syntactically, Promise is an object from which you can get messages for asynchronous operations.
The promise of the individual understanding is the container of the asynchronous operation, where each state of the asynchronous operation is placed within the container, and the promises object has a then method that allows the specified callback function to be invoked after the asynchronous task completes.
There are three states of the Promise object:
1 asynchronous operation "Incomplete" (pending)
2 asynchronous Operation "completed" (resolved)
3 Asynchronous Operation "failed" (rejected)
There are only 2 types of changes in these three states: 1 asynchronous operations from "incomplete" to "completed" 2 asynchronous operations from "incomplete" to "failed"
So the end result of the Promise object is only two kinds: 1 The asynchronous operation succeeds promise the object returns a value, the state becomes resolved 2 the asynchronous operation fails
Promise object throws an error, State becomes rejected
In addition, the promise constructor takes a function as an argument, and the two parameters of the function are resolve and reject respectively. They are two functions that are provided by the JavaScript engine and do not have to be deployed on their own.
The role of the resolve function is to change the state of the promise object from incomplete to successful (that is, from pending to resolved), to be invoked when the asynchronous operation succeeds, and to pass the result of the asynchronous operation as a parameter;
The role of the Reject function is to change the state of the promise object from incomplete to failed (that is, from pending to rejected), to be invoked when an asynchronous operation fails, and to pass the error reported by the asynchronous operation as a parameter.
After the promise instance is generated, you can use the then method to specify the callback functions for the resolved state and the rejected state, respectively.
give me a chestnut:
Window. Promise is a built-in object of JS, then we can directly take out the Var p= new Promise () to use the
At the moment, p is a promise instantiation object. Then P can go to a chained call to the Promise object's method then and to use the Promise object's state
P.then (function (Success) {
//Success
}, function (Error) {
//Failure
});
To put it simply:
There is a method under the Promise object then
Grammar:
Promise object. Then (RESOLVEDFN,REJECTEDFN)
When the state of the promise object changes to resolved, the RESOLVEDFN is executed, and if it is rejected, the REJECTEDFN object is returned after execution of then promise