yunyuyuan
2021-03-12 17:49:10 +08:00
```javascript
const arr = []
const reqList = []
// just for test
async function sleep (sec){
return new Promise(resolve => {
setTimeout(()=>{
resolve()
}, sec)
})
}
async function foo() {
return new Promise(async (resolve, reject) => {
for(let i=0; i< 5; i++){
(function (i) {
reqList.push(new Promise(resolve1 => {
const func = async ()=> {
let a = await sleep(5)
arr.push(a)
console.log(`${i} ok`)
resolve1()
}
setTimeout(func, 1000 * i )
}))
})(i)
}
Promise.all(reqList).then(()=>{
console.log('all ok')
resolve()
})
})
}
foo().then(res=> {
})
```