Promise实现队列

129 阅读1分钟

具体内容就不组织了,直接来例子更明确

function prossFun(timer){
    return  new Promise(function(resolve, reject){       
        setTimeout(function(){
        console.log(timer)
        resolve(true);
        }, 1000);
    });
}

// 队列过程
Promise.resolve().then(function(){
        return  new Promise(function(resolve, reject){       
            setTimeout(function(){
                console.log(1)
                resolve(true);
            }, 1000);
        });
    })
    .then(function(a){
         return prossFun(2)
    })
    .then(function(){
        return  new Promise(function(resolve, reject){       
            setTimeout(function(){
                console.log(3)
                resolve(true);
            }, 1000);
        });
    })
    .then(function(b){
         return prossFun(4)
    })
    .then(function(obj){
         return prossFun(5)
    })
    .then(data=>{
         return prossFun(6)
    })
    .catch(e => console.log(e));