小编jsp*_*ing的帖子

“for of”和“for wait of”之间的区别

这是一个定时器函数

const timer = (time) => {
  return new Promise((resolve, reject) => {
    console.log(`${time} timer start`);
    setTimeout(() => {
      console.log(`${time} timer end`);
      resolve();
    }, time);
  });
};
Run Code Online (Sandbox Code Playgroud)

我将用“for wait of”和“for of”来调用这个计时器函数。

首先,“等待”

async function runForAwaitOf() {
  const times = [300, 100, 700, 500];

  for await (let time of times) {
      await timer(time);
  }

  console.log('All the timers end');
}
runForAwaitOf()
Run Code Online (Sandbox Code Playgroud)

二、“为之”

async function runForOf() {
  const times = [300, 100, 700, 500];

  for (let time of times) {
      await timer(time);
  } …
Run Code Online (Sandbox Code Playgroud)

javascript

8
推荐指数
1
解决办法
3614
查看次数

标签 统计

javascript ×1