1. When Promise resolve object as a parameter when
P = const Promise.resolve (); const P1 = Promise.resolve (P); // is P const = P2 new new Promise (RES => RES (P)); // create a new object, the object state-dependent P // res (p) P1 can be regarded as the await; Resolve the await (); // . Alternatively p.then (Data => the getData ()) the then (() => p2.resolve ()) // first; p1 === the p-;! === the p-p2 // Well, p1 is the object of a fulfilled state; the state needs to run p2 obtained console.log (the p-=== p1); // to true console.log (the p-=== p2 ); // to false p1.then (() => { the console.log ( 'P1-1' ); }.) the then (() => { the console.log ( 'P1-2' ); }) the then ((). => { the console.log ( 'P1-3' ); }) p2.then (() => { // after p2.resolve to callback function the console.log ( 'P2-1' ); }) the then ((). => { the console.log ( 'P2-2' ); .}) the then (() => { the console.log ( ' P2-3 ' ); }) p.then (() => { the console.log ( '. 1-P ' ); }) the then ((). => { the console.log ( ' P-2 ' ); }).then(() then(() => { The console.log ( '. 3-P' ); }) // run results // the getData () P1-1 P -1 // Resolve () P1-2 P -2 P2 -1 P1 -3 P -3 P2 -2 P2 -3
2. When operating at Promise resolve method then the method further object Promise, the mutation step;
P3 the let; P1 = new new Promise (Resolve => { P3 = new new Promise (RES => RES ()); p3.then (() => { the console.log ( 'P3' ) Resolve (); // Resolve ( ) then the method used in the method, it becomes asynchronous execution }) }) p1.then (() => { the console.log ( 'P1-1' ); .}) then (() => { the console.log ( 'P1-2' ); }) p3.then (() => { the console.log ( 'P3-1' ) .}) the then (() => { the console.log ( 'P3-2' ) }) // results are as follows: P3 P3 -1 P1 -1 P3 -2 P1 -2
Example:
const p1 = Promise.resolve(); let p3; const p2 = new Promise(function(resolve, reject){ p3 = new Promise(res => res(p1)); p3.then(() => { //1 p3.then第一个 console.log('p3') resolve('ok'); }) }); p1.then(() => { console.log('p1-1') }).then(() => { console.log('p1-2') }).then(() => { the console.log ( 'P1-3' ) }) p2.then ( function (Data) { the console.log ( 'P2-1' ) }). the then ( function (Data) { the console.log ( 'P2-2' ) }). then ( function (Data) { the console.log ( 'P2-3' ) }) // second p3.then, p3 then trigger a state change method, triggered simultaneously performed in the order // long when p3.then (...) are triggered simultaneously p3.then ( function (Data) { the console.log ( 'P3-1' ) }). the then ( function (Data) { the console.log ( 'P3-2' ) .}) the then ( function (Data) { the console.log ( 'P3-3' ) }) // run results P1-1 P1 -2 P3 P3 -1 P1 -3 P2 -1 P3 -2 P2 -2 P3 - 3 P2 -3
3. When using the catch () method to capture exception
P1 = const Promise.resolve (); const P2 = Promise.reject (); // status Rejected p1.then (() => { the console.log ( 'P1-1' ) }) the then ((). = > { the console.log ( 'P1-2' ) }). the then (() => { the console.log ( 'P1-3' ) }) p2.then ( function (Data) { // triggers immediately, but trigger is omitted then the second function; the console.log ( 'P2-1' ) .}) then ( function (Data) { the console.log ( 'P2-2' ) .}) then ( function(Data) { the console.log ( 'P2-3' ) .}) the catch (() => { the console.log ( 'catched' ) }) // results are as follows: P1-1 // Default operation of the p2 an error callback P1-2 // default run p2 of the second error correction P1-3 // default run p2 third error callback catched