Use ES6-promise object

Promise meaning (taken from Ruan Yifeng ES6ru)

Promise is asynchronous programming a solution than traditional solutions - more rational and more powerful - callback functions and events. It was first proposed and implemented by the community, ES6 be written into the standard language, unified usage, native in the Promise object.

The so-called Promise, it simply is a container, which holds the event will end sometime in the future (usually an asynchronous operation) results. Syntactically, Promise is an object, you can get messages from the asynchronous operation of it. Promise to provide a unified API, a variety of asynchronous operations can be handled the same way.

Promise object has the following two characteristics.

State (1) is not subject to outside influence. Promise object represents an asynchronous operation, there are three states: pending (in progress), fulfilled (been successful) and rejected (failed). Only results of asynchronous operations, you can decide what kind of current state, no other operations can change that state. Promise This is the origin of the name, which in English means "commitment", he said other means can not be changed.

(2) Once the status change, it will not change, at any time this result can be obtained. Promise object changes state, only two possibilities: from pending from pending changes to become fulfilled and rejected. As long as these two things happens on the solidification state, it will not be changed and will keep the result, then called resolved (finalized). If the change has occurred, you add a callback function to Promise object will immediately get this result. This event (Event) is completely different, the characteristics of the event is, if you missed it, go listen, is not the result.

Note that, in order to facilitate the drafting, later in this chapter refers only fulfilled resolved unified state, does not contain rejected state.

With Promise object, the process may be asynchronous operation to synchronous operation expressed, to avoid deeply nested callback function. Further, Promise object provides a unified interface, so that the control of asynchronous operations easier.

Promise also has some drawbacks. First of all, you can not cancel the Promise, it will be executed immediately once the new can not be canceled midway. Secondly, if you do not set a callback function, internal Promise thrown error, it does not react to the outside. Third, when in pending status, progress is currently no way of knowing where a stage (beginning or nearing completion).

If certain events continue to occur repeatedly, in general, use Stream model is the better choice than the deployment of Promise

promise to solve the problem es callback hell
let status =  1;

function step1(resolve,reject){
    console.log('step1-start');
    if(status = 1){
        resolve('step1-success');
    }else{
        reject('step1-error');
    }
}

function step2(resolve,reject){
    console.log('step2-start');
    if(status = 1){
        resolve('step2-success');
    }else{
        reject('step2-error');
    }
}

function step3(resolve,reject){
    console.log('step3-start');
    if(status = 1){
        resolve('step3-success');
    }else{
        reject('step3-error');
    }
}

//调用
new Promise(step1).then(function(val){
    console.log(val);
    return new Promise(step2);
}).then(function(val){
        console.log(val);
        return new Promise(step3);
    }
).then(function(val){
    console.log(val);
    // return new Promise(step3);
});

Printing is as follows:

step1-start
step1-success
step2-start
step2-success
step3-start
step3-success

 

Guess you like

Origin www.cnblogs.com/Ananiah/p/11074576.html
Recommended