async / await会阻塞线程node.js [英] Will async/await block a thread node.js

查看:408
本文介绍了async / await会阻塞线程node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在node.js函数中使用 async / await 时,是否会阻塞node.js线程,直到它执行下一行代码?

When async/await used in a node.js function, will it block the node.js thread untill it executes the next line of code ?

推荐答案

async / await 不会阻止整个解释器。 node.js仍然将所有Javascript作为单线程运行,即使某些代码在 async / await 上等待,其他事件仍然可以运行其事件处理程序(所以node.js是没有阻止)。事件队列仍在为其他事件提供服务。事实上,这将是一个解决承诺的事件,该承诺将允许等待停止等待并运行以下代码。

async/await does not block the whole interpreter. node.js still runs all Javascript as single threaded and even though some code is waiting on an async/await, other events can still run their event handlers (so node.js is not blocked). The event queue is still being serviced for other events. In fact, it will be an event that resolves a promise that will allow the await to stop awaiting and run the following code.

这样的代码:

await foo();            // foo is an async function that returns a promise
console.log("hello");

类似于:

foo().then(() => {
    console.log("hello");
});

所以, await 只需输入以下代码在该范围内成为一个不可见的 .then()处理程序,其他所有内容的工作方式与使用 .then()实际编写的内容完全相同 handler。

So, await just puts the following code in that scope into an invisible .then() handler and everything else works pretty much the same as if it was actually written with a .then() handler.

所以, await 允许你保存<$的写作c $ c> .then() handler并为代码提供同步外观(尽管它并不是真正的同步)。最后,它是一种速记,可让您使用较少的代码行编写异步代码。人们确实需要记住,任何可以拒绝的承诺必须在它周围的某个地方有一个try / catch来捕获和处理拒绝。

So, await allows you to save the writing of the .then() handler and gives the code a synchronous look to it (though it isn't really synchronous). In the end it's a shorthand that lets you write async code with fewer lines of code. One does need to remember though that any promise that can reject must have a try/catch somewhere around it to catch and handle that rejection.

从逻辑上讲,你可以想到什么node.js在执行以下函数时遇到 await 关键字时会执行以下操作:

Logically, you can think of what node.js does when it encounters an await keyword when executing a function as the following:


  1. 进行函数调用

  2. 解释器看到该函数声明为 async ,这意味着它将始终返回一个promise 。

  3. 解释器开始执行该功能。

  4. 当遇到 await 关键字时,它暂停该函数的进一步执行,直到等待的promise被解决。

  5. 该函数然后返回一个未解决的promise。

  6. 此时,解释器继续执行函数调用之后的任何内容(通常是 fn()。然后()跟随其他代码行)。然后 .then()处理程序尚未执行,因为承诺尚未解决。

  7. 在某些时候这个Javascript序列完成并将控制权返还给解释器。

  8. 解释器现在可以自由地从事件队列中提供其他事件。遇到 await 关键字的原始函数调用仍然暂停,但现在可以处理其他事件。

  9. 在未来的某个时间点,正在等待的原始承诺得到解决。当在事件队列中处理该事件时,先前挂起的函数在 await 之后继续在该行上执行。如果还有等待语句,那么函数执行将再次暂停,直到该保证结算为止。

  10. 最后函数命中 return 语句或到达函数体的末尾。如果有一个返回xxx 语句,那么将评估 xxx ,其结果将成为承诺的已解决值此 async 函数已返回。该函数现在已完成执行,并且之前返回的承诺已得到解决。

  11. 这将导致附加的任何 .then()处理程序承诺此函数先前返回被调用。

  12. 在那些 .then()处理程序运行之后,这个<的工作code> async 函数终于完成了。

  1. Function call is made
  2. The interpreter sees that the function is declared as async which means that it will always return a promise.
  3. The interpreter starts executing the function.
  4. When it encounters an await keyword, it suspends further execution of that function until the promise that is being awaited resolved.
  5. The function then returns an unresolved promise.
  6. At this point, the interpreter continues executing whatever comes after the function call (usually a fn().then() follow by other lines of code). Then .then() handlers are not executed yet because the promise is not yet resolved.
  7. At some point this sequence of Javascript finishes and returns control back to the interpreter.
  8. The interpreter is now free to serve other events from the event queue. The original function call that ran into an await keyword is still suspended, but other events can be processed now.
  9. At some future point, the original promise that was being awaited gets resolved. When it's time for that to get processed in the event queue, the previously suspended function continues executing on the line after the await. If there are any more await statements, then the function execution is again suspended until that promise resolves.
  10. Eventually the function hits a return statement or reaches the end of the function body. If there is a return xxx statement, then the xxx is evaluated and its result becomes the resolved value of the promise that this async function has already returned. The function is now done executing and the promise it previously returned has been resolved.
  11. This will cause any .then() handlers attached to the promise that this function previously returned to get called.
  12. After those .then() handlers run, the job of this async function is finally done.

所以,虽然整个解释器没有block(其他Javascript事件仍可以提供服务),包含 await 语句的特定 async 函数的执行是暂停,直到等待的承诺得到解决。重要的是要理解上面的步骤5。当第一个 await 被命中时,该函数在执行此函数后立即返回未解决的promise和代码(在承诺等待已解决)。出于这个原因,整个翻译都没有被阻止。执行继续。只有一个函数的内部才会被暂停,直到承诺得到解决。

So, while the whole interpreter doesn't block (other Javascript events can still be serviced), the execution of the specific async function that contains the await statement was suspended until the promise that was being awaited resolved. What's important to understand is step 5 above. When the first await is hit, the function immediately returns an unresolved promise and code after this function is executed (before the promise being awaited is resolved). It's for this reason that the whole interpreter is not blocked. Execution continues. Only the insides of one function are suspended until a promise is resolved.

这篇关于async / await会阻塞线程node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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