ES6 Promise objects (seven)

A, Promise introduced
1, Promise simply is a container, which holds the event will end sometime in the future ( usually an asynchronous operation ) result
2, Promise can be asynchronous operation to synchronous operation of the process to express, to avoid deeply nested callback function. Further, Promise object provides a unified interface, so that the control of asynchronous operations easier

two, basic usage [ Note asynchronous operating environment and associated packages installed using }
in a node jsdom environment need to use ajax, jqury package
configuration constants:

    // Create window environment 
    const} = {JSDOM the require ( 'jsdom' ); 
    const {} window = new new JSDOM ( '<DOCTYPE HTML!>' );
     // Get jQuery function 
    const $ = require ( 'jquery' ) (window );

1, Promise constructor accepts a function as a parameter, the function of the two parameters are a function of resolve and reject function [ arguments supplied by the JavaScript engine, do not deploy their own ]
eg:

    let promise = new Promise((resolve,reject)=>{});

a, role resolve function:
the state of the object from the Promise "unfinished" to "success" [ from Pending changes Resolved ], upon successful call asynchronous operation, and the result of the asynchronous operation, to pass out as a parameter

B, reject the role of function:
the state of the object from the Promise "unfinished" to "failed" [ from Pending changes Rejected ], invoked when an asynchronous operation fails, and the asynchronous operation errors may be, as a parameter to pass out

Promise example after generation, the method may be used examples were then specify a callback function Resolved and Rejected state [state is generally recommended to use catch and then specify a callback function separately ]

then (fun1, fun2)
method parameters:
A, fun1: to Resolved state Promise object callback
b, fun2: Rejected state Promise object callback

catch (fun)    [ which is an alias then (null, fun2) method ]
Method parameters :
Fun: Promise callback object Rejected state

NOTE:
general then performed using Resolved state callback function is performed using the catch Rejected state callback function without directly using the callback function then two states

finally (fun)     [ Promise objects regardless of state, will be performing the method ] ( generally used for resource recovery )
EG:

    Promise = the let new new Promise ((Resolve, Reject) => {
         // internal parameter function operation is generally performed asynchronously 
        $ .ajax ({ 
            URL: '' , 
            Method: '' , 
            Data: [], 
            Success (RES) { 
                Resolve ( RES); 
            }, 
            error (error) { 
                Reject (error); 
            } 
        }); 
    }); 
    // callback function Promise constructor asynchronously executed successfully resolve () method of 
    promise.then ((RES) => {} );
     // performed asynchronously Promise constructor fails reject (callback) method 
    . Promise the catch ((error) =>{});
     // regardless of the state of the object Promise, the method will be executed 
    Promise. The finally (() => {});
     // The above method may also be used in the form of the following call 
    // promise.then ((RES) => { ..}) catch ((error ) => {}) finally (() => {});

   
Three, Promise correlation function method
1, Promise resolve ().
Parameter Type :
A, is a parameter Promise example     { return this instance Promise ]
eg:

    // passed is Promise objects, no operation, is returned as the example Promise Promise 
    the let P = Promise.resolve (Promise); 

    p.then ((RES) => { 
        the console.log (RES); 
    }). The catch ((error) => { 
        console.log (error, '-------' ); 
    }). finally (() => {
         // regardless of the success or failure of an asynchronous operation will finally perform functions 
        console.log ( 'the finally' ); 
    });


b, the object parameter is a thenable [ thenable the object refers to the object has a method then ]
Promise.resolve this method will thenable Promise objects into an object, then immediately execute this method then thenable object
eg:

    obj = the let { 
        name: 'zhangsan' , 
        then () { 
            (the console.log 'then the method obj' ); 
        } 
    }; 

    the let P = Promise.resolve (obj);     // direct execution method then object obj , then the method is not performed in the instance of an object p 
    p.then (() => {         // this method does 
        console.log ( 'p3 then the method " );   
    });


c, a thenable parameter is not an object or objects is simply not     [ return Promise objects a new, state Resolved, then execute the callback function ]
eg:

    // perform the resolve function, i.e. execution of the callback function then performs the resolve function [, Promise was converted from the object to resolve pending state status] 
    the let P = Promise.resolve ( 'Hello');   // return resolve state Promise [Object] internal functions performed resolve resolve print string 
    p.then (() => { 
        the console.log ( 'resolve' ); 
    }). the catch (() => { 
        the console.log ( 'Reject' ); 
    } );


d, no parameters     [ Promise object returns a status Resolved ]

2, Promise.all ()     { return multiple instances Promise packaged into a new instance of Promise ]
method parameters:
parameter is an array composed of a plurality of instances Promise
method Description:
a, a plurality of instances Promise, Promise packaged into a new instance, and returns the new packaging Promise example
B, if the parameter array all Promise instance state is resolved , a new instance of packaging state was state to resolve ,
callback method then instantiates new packaging resolve in the data parameters for all instances successful return request data
C, if the parameter array encountered there Promise instance when the state is rejected , a new instance of the package state is the state rejected , no need to look behind the instance state,
the callback function parameter catch examples of new packaging methods reject the failed instance parameter data for rejected status request data
eg:

    // P1, P2, P3 for the instance object Promise 
    var p = Promise.all ([P1, P2, P3]); 
    
    // state p in a new instance of an array parameter information returned in the results of Example fastest status parameters [i.e. array fastest instance object information acquired] 
    p.then ((RES) => { 
        the console.log (RES);     // P1, set information p2, p3 in the resolved state returned 
    }). the catch ((error) => { 
        the console.log (error)     // parameters assigned to the first example of the status information back rejected 
    });


3, Promise.race ()     { return multiple instances Promise packaged into a new instance of Promise ]
method parameters:
parameter is an array consisting of a plurality of instances Promise
Method Description:
A, new packaging Promise instance state parameter array returns information results fastest instance state
b, the new data message instance corresponding method of packaging is the change in the parameters Promise array of this example related examples return information
eg:

    // P1, P2, P3 for the instance object Promise 
    var p = Promise.race ([P1, P2, P3]); 
    
    // state p in a new instance of an array parameter information returned in the results of Example fastest status parameters [i.e. array fastest instance object information acquired] 
    p.then ((RES) => { 
        the console.log (RES);     // successful implementation example of the fastest data parameters returned array 
    }). the catch ((error) = > { 
        the console.log (error)     // parameter data array fails fastest instance returned 
    });


 

Guess you like

Origin www.cnblogs.com/nzcblogs/p/11373856.html
Recommended