如何在等待承诺解决的同时执行代码?
查看此示例代码:
var arrayToSend = [1,2,3,4,5,6,7,8,9]
function send(x){ // 'sends' the value. resolves after 1 second.
return new Promise(resolve=>{
setTimeout(()=>{
resolve()
console.log('sent '+x)
},1000)
})
}
function calculate(x){return x**2} // just an example, there would be something expensive here.
async function loopAndSendArray(){
for (let val of arrayToSend){
let calculatedVal = calculate(val) //calculate this when waiting for the send promise to finish BUT don't send the value until it's resolved
await send(calculatedVal)}
}
我希望脚本呼叫calculate()
在等待send()
前一个值的下一个值时要完成(以节省时间,否则将花费在承诺后计算它)。它不会以下一个值调用下一个send()
,直到解决第一个值。
Take a look at this example code:
var arrayToSend = [1,2,3,4,5,6,7,8,9]
function send(x){ // 'sends' the value. resolves after 1 second.
return new Promise(resolve=>{
setTimeout(()=>{
resolve()
console.log('sent '+x)
},1000)
})
}
function calculate(x){return x**2} // just an example, there would be something expensive here.
async function loopAndSendArray(){
for (let val of arrayToSend){
let calculatedVal = calculate(val) //calculate this when waiting for the send promise to finish BUT don't send the value until it's resolved
await send(calculatedVal)}
}
I'd like the script to call calculate()
for the next value when it's waiting for the send()
promise of the former value to finish (to save time that would otherwise be spent calculating it after the promise). It would not call the next send()
with the next value until the first one is resolved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与其等待
发送
的调用,不如将调用分配给外部变量,然后在下一个迭代中调用计算
后等待等待。无论计算是否需要比网络请求更多的时间,如果必须在串行中提出请求(如当前代码所做的那样),这都是最佳的。如果可以并行提出请求,并且计算时间少于网络请求,则可以一次计算并发送多个(或全部)。
Instead of awaiting the call to
send
, assign the call to an outside variable, and thenawait
it after callingcalculate
in the next iteration.This is optimal no matter whether the calculation takes more or less time than the network request, if the requests have to be made in serial (as your current code is doing). If the requests can be made in parallel and the calculation takes less time than the network request, you could calculate and send multiple (or all) at once.
由于
async
/等待
是固有的顺序,因此您想以不同的顺序进行操作,但是此代码应该这样做:There is no nice way to implement this, since
async
/await
is inherently sequential and you want to do things in a different order, but this code should do it: