实现Promise串行

2020-06-01  本文已影响0人  我才不鸟你呢

前几天做总结,看到这个问题,多个Promise串行,梳理一下

var promiseArray = [new Promise((resolve,reject)=>{console.log('1')}),new Promise((resolve,reject)=>{console.log('2')}),new Promise((resolve,reject)=>{console.log('3')}),new Promise((resolve,reject)=>{console.log('4')}),new Promise((resolve,reject)=>{console.log('5')})];

1.原始简单方法

let arry1= [];

for(var i = 0; i<promiseArray.length-1; i++){

    promiseArray[i]().then(result=>{

arry1.push(result);  

  console.log(result);

});

console.log(arry1);

}

2.借助 async/await串行

let p = Promise.resolve();

for(var i = 0; i<promiseArray.length-1; i++){

p(promiseArray[i]()).then(result=>{

arr1.push(result)

})

console.log(arry1);

}

上一篇下一篇

猜你喜欢

热点阅读