let arr = []
async function foo() {
return new Promise(async (resolve, reject) => {
for(let i=0; i< 100; i++){
(function (i) {
setTimeout(async ()=> {
let a = await xxx(i)
arr.push(a)
}, 1000 * i * 5)
})(i)
}
A => setTimeout(resolve, 10000, arr)
})
}
B => foo().then(res=> {
...
})
遍历个数组调接口,接口限制 1 分钟最多只能调 20 次.
怎么能让这段代码改成同步执行,等待遍历完成后 我在 A 那不用写等待时间
B 直接获取到 arr 的数据
async function foo() {
return new Promise(async (resolve, reject) => {
for(let i=0; i< 100; i++){
(function (i) {
setTimeout(async ()=> {
let a = await xxx(i)
arr.push(a)
}, 1000 * i * 5)
})(i)
}
A => setTimeout(resolve, 10000, arr)
})
}
B => foo().then(res=> {
...
})
遍历个数组调接口,接口限制 1 分钟最多只能调 20 次.
怎么能让这段代码改成同步执行,等待遍历完成后 我在 A 那不用写等待时间
B 直接获取到 arr 的数据