串行等待请求在并行运行的返回中? [英] serial await requests in a return run in parallel?
本文介绍了串行等待请求在并行运行的返回中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
与某人进行讨论并遇到了这种奇怪情况:
Having a discussion with someone and came across this oddity:
const wait = async () => new Promise(resolve => setTimeout(resolve, 1000));
async function case1() {
const {a, b} = {a: await wait(), b: await wait()};
return {a, b};
}
async function case2() {
return {a: await wait(), b: await wait()};
}
async function case3() {
const {a, b} = {a: wait(), b: wait()};
return {a: await a, b: await b};
}
async function case4() {
const {a, b} = {a: wait(), b: wait()};
const {c, d} = {c: await a, d: await b};
return {c, d};
}
function test() {
const start = new Date();
case1().then(() => console.log('case1:', +new Date() - start));
case2().then(() => console.log('case2:', +new Date() - start));
case3().then(() => console.log('case3:', +new Date() - start));
case4().then(() => console.log('case4:', +new Date() - start));
}
case1
和case2
都在2秒内运行. case3
和case4
在1秒钟内运行.
case1
and case2
both run in 2 seconds. case3
and case4
run in 1 second.
是否存在一些奇怪的隐式Promise.all
或其他内容?
Is there some weird implicit Promise.all
or something??
推荐答案
在不使用case3
和case4
处的await
的情况下调用函数wait()
.就是这样.
You call the function wait()
without utilizing await
at case3
and case4
. That is the difference.
这篇关于串行等待请求在并行运行的返回中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文