Promise和async/await

1, promise objects

promise object has three states: pending (in progress), fulfilled (been successful) and rejected (failed). Promise to change the state of the object, only two possibilities: from pending from pending changes to become fulfilled and rejected.

const promise = new Promise(function(resolve, reject) {
  if (){
    resolve(value);
  } else {
    reject(error);
  }
});

promise constructor accepts a function as a parameter, the function of the two parameters are the resolve and reject, they are two functions provided by the JavaScript engine, do not have to deploy.

function will resolve the state of the object from the promise "unfinished" to "success" (i.e., becomes resolved from pending) data may be passed as a parameter out. Reject function is to effect the state of the object from promise "unfinished" to "failed" (i.e., changed from pending rejected), certain error messages may also be passed as a parameter out.

Since the new Promise executed immediately, and then wrapped in a layer can be a function of the outside promise:

function timeout(ms) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, ms, 'done');
  });
}

timeout(100).then((value) => {
  console.log(value);
});

 

1.1, then () method

Promise Examples are an object, not a function. After promise instance generation, you can specify a callback function resolved state by state and then rejected Methods.

let promise = new Promise(function(resolve, reject) {
  if (){
    resolve(value);
  } else {
    reject(error);
  }
});

promise.then(function(value) {
  console.log(value)
}, function(error) {
  console.log(error)
});

The method then can accept two callback functions as arguments. The first callback function is called when the state of the promise object becomes resolved, the second callback function is called when the state of the promise object becomes rejected, the second function is optional and not required to provide. Both functions accept the value of the object promise came as a parameter.

 

1.2, then () method for writing chain

The method then returns a new promise instance, can be written using the chain, i.e. then back again method then calls another method.

1.2.1, then the method which returns a value determined

Inside a then () method you can return a certain "value", then this time will be this exact values ​​passed a new default instance of Promise, and the Promise example immediately set to fulfilled state, the return of value passed as a parameter resolve out the method, then for subsequent use in the method.

P1 = the let new new Promise ((resolve, Reject) => { 
    resolve ( 'AAA' ) 
}) 
p1.then ((data) => { 
    data = data + 'BBB'
     return data  // In this case the data will resolve as parameters passed out 
}) the then ((Val) =>. { 
    the console.log (Val + 'Sucess' ); 
}, (ERR) => { 
    the console.log (ERR + 'error' ); 
}) 

// output: aaabbb sucess

 

1.2.1, then the method returns a promise which instance

If the method then returns inside of an object or a promise, this time after a callback function, it will wait for the promise of the object state changes, will be called.

// first asynchronous task 
function A () {
   return  new new Promise ( function (Resolve, Reject) { 
     Resolve ( "Function A" ); 
  }); 
} 
// The second asynchronous task 
function B (Data_A) {
   return  new new Promise ( function (Resolve, Reject) { 
     the console.log (Data_A); 
     Resolve ( "function B" ); 
  }); 
} 

// successive calls 
A () the then (. function (Data) {   
     return B (Data);       / / in this case then the method returns a promise inside the object, the latter then waits for the state of the object changes promise will be called
.}) the then ((Data) => { 
    the console.log (Data + 'sucess' ) 
}, (ERR) => { 
    the console.log (ERR 'Rejected' + ) 
}) 
// Output: a function sucess function b

Then waiting in front of the last above function inside a promise then function object state changes, if becomes resolved, a callback function is called first, if the state is changed to Rejected, the second callback function is invoked.

1.2.1, then the method does not return inside

Then if the method does not return data, so back then you will not have access to previous data, but then the latter method can still perform.

// first asynchronous task 
function A () {
   return  new new Promise ( function (Resolve, Reject) { 
     Resolve ( "Function A" ); 
  }); 
} 
// The second asynchronous task 
function B (Data_A) {
   return  new new Promise ( function (Resolve, Reject) { 
     the console.log (Data_A); 
     Resolve ( "function B" ); 
  }); 
} 

. A () the then ( function (Data) {    
    the console.log (Data)     // not return 
} ) .then ((Data) => { 
    the console.log (Data + 'Sucess') 
}, (ERR) => { 
    the console.log (ERR + 'Rejected' ) 
}) 
// Output: a function undefined sucess

 

1.3, catch () method

Promise.prototype.catchMethod .then(null, rejection)or .then(undefined, rejection)alias for the specified callback function an error occurs.

 

Guess you like

Origin www.cnblogs.com/wenxuehai/p/11306245.html
Recommended