技术解析

promise + settimeout 的问题
0
2021-08-10 00:51:21
idczone
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 的数据
for 里可以 await,for 结束后直接 resolve,当然,上下文得在 async 里

现在不就是 for 里 await 吗

不是,套了一个普通 function,一个 setTimeout 啊

没懂你什么意思

```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=> {
})
```

没缩进是真的难受

没缩进看着不难受么?

这不是标准面试题里面的 EventLoop 问题么

都 let 了 还用什么 iife

一分钟调 20 次,是指每调得等 3 秒,还是单纯的 60 秒内不超过 20 就行?如果是后者,那么周期是从哪里开始算呢?比如,第一分钟的最后一秒连续调了 20 次,第二分钟的第一秒连续调了 20 次,但只看这两秒的话,两秒钟就有了 40 次,这种情况符合你说的一分钟 20 次吗?

没有时间限制的版本(其实用 Promise.all 让它并发就行,但这里为了写法一致就写成了同步):
https://imgtu.com/i/6djJ0g
三秒钟只能请求一次(搞一个 setTimeout 一起让 Promise.all 等,两者必须都 resolve 一次循环才结束):
https://imgtu.com/i/6djGnS

@ljpCN

图片没出来
https://s3.ax1x.com/2021/03/13/6djJ0g.png
https://s3.ax1x.com/2021/03/13/6djGnS.png

感谢老哥

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服