是什么使`async/await`语句在ES6中顺序运行而又并行运行? [英] What makes `async/await` statements run sequentially vs in parallel in ES6?

查看:87
本文介绍了是什么使`async/await`语句在ES6中顺序运行而又并行运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过了线程 await之间的任何区别Promise.all()和多个等待?,所以我对Promise.all和多个等待很清楚.

I have already gone through the thread Any difference between await Promise.all() and multiple await?, so I am clear about Promise.all and multiple awaits.

不过,对于以下两种情况我还是不太清楚.

Still, I am not very clear about the below 2 scenarios.

在案例1中,为什么它顺序执行(花费10s),而在案例2中,它并行执行(花费4s)?

In Case 1 why does it execute sequentially (takes 10s) whereas in Case 2 it executes in parallel (takes 4s)?

情况1:

function promiseWait(time) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
}

async function test1() {
  var t0 = performance.now()

  var a = await promiseWait(1000)
  var b = await promiseWait(2000)
  var c = await promiseWait(3000)
  var d = await promiseWait(4000)

  var t1 = performance.now()
  console.log("Call to doSomething took " + (t1 - t0) + " milliseconds."); //takes 10secs
  
}
test1()

情况2:

function promiseWait(time) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
}

async function test2() {
  var t0 = performance.now()

  const p1 = promiseWait(1000);
  const p2 = promiseWait(2000);
  const p3 = promiseWait(3000);
  const p4 = promiseWait(4000);

  const a = await p1;
  const b = await p2;
  const c = await p3;
  const d = await p4;

  var t1 = performance.now()
  console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
}

test2()

推荐答案

在第一种情况下,由于在每次调用 promiseWait 之前先使用 await ,甚至开始执行下一次对 promiseWait 的调用需要等待,直到对 promiseWait 的第一次调用完全结束.所以您会看到顺序执行.

In the first case because of the await before each call to the promiseWait, to even start executing the next call to the promiseWait it needs to wait until the first call to the promiseWait is finished completely. So you see sequential execution.

在第二种情况下,您已经在开始等待之前调用了 promiseWait 函数的 all .因此, promiseWait 已经开始执行,然后您要一个接一个地等待结果.

In the second case you have already invoked all the promiseWait functions before you start awaiting them. So the promiseWait has already started executing, then you are awaiting the results one after another.

在第一种情况下的实现明智的做法是,对 setTimeout 的下一次调用必须等待,直到第一个 setTimeout 到期为止.因此,第二个,第三个和第四个计时器需要等待,直到第一个计时器到期并解决了承诺后才能进行调度.

Implementation wise in the first scenario, the next call to the setTimeout has to wait until the first setTimeout expires. So the second, third and the fourth timers need to wait until the first timer expires and resolves the promise in order to be scheduled.

在秒的情况下,您将 setTimeout 调用安排为一个接一个,因此计时器为 所有已排队的 .然后,您只是在等待计时器到期并一一解决您的诺言.

In the seconds case you schedule the setTimeout calls one after the other, so the timers are all already queued. Then you are just awaiting for the timers to expire and resolve your promise one by one.

这篇关于是什么使`async/await`语句在ES6中顺序运行而又并行运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆