简体   繁体   English

await 究竟做了什么,它与微任务队列有什么关系?

[英]What await does exactly and does it have anything to do with microtask queue?

I am new to JavaScript and I am trying to understand what await does and what is its relation with microtask queue.我是 JavaScript 新手,我试图了解await的作用以及它与微任务队列的关系。 The following code is just for clarification purpose only.以下代码仅用于说明目的。

async function afunc() {
  console.log("a1");
  const b = bfunc();
  console.log("a2");
  b.then((value) => {
    console.log(value);
  });
  return "a value";
}

async function bfunc() {
  console.log("b1");
  await console.log("waiting");
  console.log("b2");
  return "b value";
}

const a = afunc();

a.then((value) => {
  console.log(value);
});

console.log("script end");

And the results are:结果是:

a1
b1
waiting
a2
script end
b2
a value
b value

On MDN it says在 MDN 上它说

The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment. await 表达式导致异步函数执行暂停,直到 Promise 被解决(即完成或拒绝),并在完成后恢复异步函数的执行。

When does this 'resume' happen exactly?这个“简历”到底是什么时候发生的? And does it has anything relatable to microtask queue in terms of execution order?就执行顺序而言,它有什么与微任务队列相关的东西吗?

Also, I expect b value to show up before a value since it seems that b.then() will be pushed in microtask queue before a.then() is, and the actual output confused me a lot.另外,我希望b value出现在a value之前,因为b.then()似乎会在a.then()之前被推送到微任务队列中,而实际输出让我很困惑。

Any help would be much appreciated!任何帮助将非常感激!

Edit : If you remove await in bfunc, the result would be like:编辑:如果您在 bfunc 中删除await ,结果将是:

a1
b1
waiting
b2
a2
script end
b value
a value

When MDN says resume execution of the async function , it means that it will wait for the function inside to be completed, then continue running the outer function.当 MDN 说resume execution of the async function时,表示它将等待内部函数完成,然后继续运行外部函数。

For example:例如:

async function outer() {
    console.log("outer function start");
    await inner();
    console.log("outer function end");
}

async function inner() {
    console.log("inner function start");
    await new Promise(resolve => setTimeout(() => resolve(), 1000));
    console.log("inner function end");
}

outer();

Will log:将记录:

outer function start
inner function start
* 1 second pause *
inner function end
outer function end

I think the reason you are seeing the logs out of order is because you are running b.then , but there is also code outside of the async callback, so the code afterwards isn't necessarily run in order.我认为您看到日志乱序的原因是因为您正在运行b.then ,但是异步回调之外还有代码,因此之后的代码不一定按顺序运行。

JavaScript promises work because of microtasks, so they are related. JavaScript Promise 之所以起作用是因为微任务,所以它们是相关的。

JavaScript promises and the Mutation Observer API both use the microtask queue to run their callbacks. JavaScript Promise 和 Mutation Observer API 都使用微任务队列来运行它们的回调。 - MDN -MDN

I hope this helps!我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM