简体   繁体   English

异步 function 何时实际返回待处理的 promise?

[英]When does async function actually return a pending promise?

I googled the question so many times but the only answer I see to this question is:我用谷歌搜索了很多次这个问题,但我看到的唯一答案是:

Async function always returns a promise异步 function 总是返回 promise

but nowhere have I found anything that answers when does that actually happen.但是我在任何地方都没有找到任何可以回答的问题。

Is it as soon as it goes into the async function or as soon as it encounters the first await keyword?是一进入异步 function 还是一遇到第一个await关键字?

I have just written this piece of code:我刚刚写了这段代码:

async function main() {
  console.log(true);
  let p = await new Promise(r => setTimeout(r, 2000, 100));
  return p;
}
async function f() {
  inner(await main())

  function inner(d) {
    console.log(d + 10)
  }

  console.log('done')
}

f();

The most important line is:最重要的一行是:

inner(await main())

main() will be executed first because of the higher priority a function call has in the precedence table and as async function returns a promise, it returns a pending promise! main()将首先执行,因为 function 调用在优先级表中具有更高的优先级,并且异步 function 返回一个 promise,它返回一个未决的承诺!

But to return that, we must execute the main() first, so it goes into the main execution context and sees the console.log(true);但要返回它,我们必须先执行 main(),所以它进入主执行上下文并看到console.log(true);

Now is that where the pending promise is returned by the main() ?现在是main()返回未决 promise 的地方吗?

Or it will log that and then it'll reach the await keyword and that's when it'll return a pending promise?或者它会记录它,然后它会到达await关键字,那时它会返回一个待处理的 promise?

What if this was our code:如果这是我们的代码怎么办:

async function main() {
  (function thatTakes30SecondsToFinish() {
    // some time consuming task
  }())
  let p = await new Promise(r => setTimeout(r, 2000, 100));

  return p;
}

Now in this case, will this main() in this line inner(await main()) immediately return a pending promise or it'll take 30 seconds before returning a pending promise?现在在这种情况下,此行inner(await main()) main()的 main() 是否会立即返回一个挂起的 promise 或者在返回一个挂起的 promise 之前需要 30 秒?

Now my guess is:现在我的猜测是:

This line inner(await main()) will immediately return a pending promise but the code inside main() will continue to execute until it gets to the first await keyword, is that correct?这一行inner(await main())将立即返回一个挂起的 promise 但 main() 中的代码将继续执行,直到它到达第一个 await 关键字,对吗?

An asynchronous function runs synchronously till it reaches the first await (or the end of the function).异步 function 同步运行,直到它到达第一个await (或函数结束)。 Then it returns a new unresolved Promise, that will be resolved somewhen later when the function finished execution.然后它返回一个新的未解析的 Promise,稍后当 function 完成执行时,它将被解析。 The execution of the asynchronous function then halts, and synchronous execution of the code that called the async function continues.然后异步 function 的执行停止,调用异步 function 的代码的同步执行继续。

this line inner(await main()) will immediately return a pending promise but the code inside main() will continue to execute until it gets to the first await keyword, is that correct?这一行 inner(await main()) 将立即返回一个挂起的 promise 但 main() 中的代码将继续执行,直到它到达第一个 await 关键字,对吗?

No. First main() will be executed synchronously till the code inside the async main function reaches an await , then it'll return a promise and execution of f continues.不会。首先main()将同步执行,直到异步主 function 中的代码到达await ,然后它将返回 promise 并继续执行f As then the await gets reached f also itself returns a new Promise and stops execution.然后await到达f本身也返回一个新的 Promise 并停止执行。

Semantic details can be found in the specification .语义细节可以在规范中找到。

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

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