本文介绍了等待同步功能是否会同步返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我不熟悉的内部实用程序库中导入了一个函数.该库没有任何文档,由于其名称getUserDetails,我只是假设它是异步的.我以为它正在执行http请求.

I imported a function from an internal utility library that I am unfamiliar with. There is no documentation for this library and I just assumed it was asynchronous due to its name getUserDetails. I thought it was doing an http request.

我在这样的异步函数中使用了它

I used it in an async function like this

async function getAllUserInfo(event) {
    const details = await getUserDetails(event);
    // other stuff
}

我的假设是错误的.一位同事指出这不是异步的.我最终对其进行了更改,但是当我使用不正确时,它仍然可以正常工作.我能够等待一个同步函数,它返回了正确的数据.

I was wrong in my assumption. A co-worker pointed out that is was not asynchronous. I ended up changing it, but when I was using it incorrectly it still worked. I was able to await a synchronous function and it returned the correct data.

我的问题是关于它是如何工作的.在同步函数上加前缀和等待是否会使它在下一个滴答声中解析,还是像同步函数一样立即返回?

My question is in regards to how it worked. Does prepending and await on a synchronous function make it resolve on the next tick, or does it return immediately like a synchronous function should?

推荐答案

之所以有效,是因为await不需要将其操作数作为承诺!如果不是promise,它将返回等待的表达式的值.

It worked because await does not require its operand to be a promise! It returns the value of the awaited expression if it is not a promise.

请参见 await操作员的文档

重要的部分是:

[rv] = await expression;

expression
    A Promise or any value to wait for.
rv
    Returns the fulfilled value of the promise, or the value itself if it's not a Promise.

在您的情况下,getUserDetails没有返回承诺,而是返回了一些常规用户详细信息,因此await表达式只是返回了这些详细信息,就像操作员根本不在那儿一样.

In your case getUserDetails did not return a promise, but rather some regular user details, so the await expression just returned those details, just as if the operator was not there at all.

但是,即使getUserDetails是同步的,在异步函数中在其前面加上await也会放弃对其调用者的控制,而在await之后的回调部分" .这是一个示例脚本:

However, even though getUserDetails is synchronous, preceding it with await in your async function will give up control to its caller, and the "callback portion" after the await is picked up later. Here is an example script:

function f() {
  console.log('In f');
}

async function g() {
  console.log('Starting g');
  await f();
  console.log('Finishing g');
}

console.log('Starting the script')
g();
console.log('Finishing the script')

注意脚本的输出:

$ node asynctest.js
Starting the script
Starting g
In f
Finishing the script
Finishing g

请注意await如何调用"paused" g,直到主块完成后才能恢复!因此await did 会起作用.如果您没有在此处等待,那么您将在完成脚本"之前看到"Finishing g".试试吧!

Notice how the await call "paused" g, which was not able to resume until the main block finished! So the await did have an effect. If you did not put the await there, then you would have seen "Finishing g" before "Finishing the script". Try it out!

造成这种结果的原因是,即使可以为await提供一个不会产生承诺的表达式,JS也会将非承诺操作数转换为立即解析为该值的承诺.因此,仍然会创建一个Promise,将等待后的部分视为回调,直到当前执行流程完成后才能运行.

BTW the reason for the effect is that even though await can be given an expression that does not produce a promise, JS will turn a non-promise operand into a promise immediately resolved to that value. So a promise is still created and the part after await is treated as a callback, which cannot be run until the current execution flow finishes.

这篇关于等待同步功能是否会同步返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:14