简体   繁体   English

异步调用 function 并返回多个

[英]async call to function with multiple returns

An async function I am dealing with, returns multiple values in array我正在处理的异步 function,返回数组中的多个值

const funcName = async function (arg) {
  ...
  return [val1, val2];
}

While calling this function, I find that it is not possible to directly access individual return values in a syntax:在调用此 function 时,我发现无法直接访问语法中的单个返回值:

result1 = await funcName(arg)[0]  
result2 = await funcName(arg)[1]

The only way (known to me so far) it works, is to break it down:它起作用的唯一方法(到目前为止我知道)是将其分解:

result = await funcName(arg)
result1 = result[0]
result2 = result[1]

Is there a way to "collect" the return directly in a single LOC?有没有办法直接在一个 LOC 中“收集”退货?
(closely, like r1 = await fn(x)[0] ) (很接近,就像r1 = await fn(x)[0]
This works obviously for sync variant, and keeps code much precise, clean, while readable.这显然适用于同步变体,并保持代码非常精确、干净、可读。

Bonus ask: any clue why direct array access is not supported?奖金问题:任何线索为什么不支持直接数组访问?
By intent, or just missed out?有意为之,还是只是错过了?

const [result1 ,result2]= await funcName(arg)

You can use array destructuring :您可以使用数组解构

const [val1, val2] = await funcName(arg);

The syntax for the await operator is await运算符的语法是

await UnaryExpression

Which means that the entire expression will be evaluated before handing over to await .这意味着将在移交给await之前评估整个表达式。 Here is a breakdown of what happens when you have await funcName(arg)[0]这是您await funcName(arg)[0]时发生的情况的细分

  1. funcName(arg) returns a promise. funcName(arg)返回一个 promise。

  2. [0] is the square brackets notation used on the promise. [0]是 promise使用的方括号符号。 So, it's getting the property 0 from the promise object.因此,它从 promise object 获取属性0

  3. Since there is no such property, the result of the expression is undefined .由于没有这样的属性,表达式的结果是undefined

  4. await is called on undefined .undefined上调用await Since this isn't a promise, it's treated as if a Promise.resolve(undefined) and so there is nothing to wait to complete.由于这不是 promise,因此它被视为Promise.resolve(undefined) ,因此无需等待完成。

  5. Code execution continues before the actual promise object from step 1. is complete.在步骤1中的实际 promise object 完成之前继续执行代码。

You can force await to consider the returned promise, if you surround the correct part of the expression in brackets or to only use it on the expression that returns a promise:您可以强制await考虑返回的 promise,如果您将表达式的正确部分括在括号中或仅在返回 promise 的表达式上使用它:

 const simpleAsync = async function () { return ["one", "two"]; } async function main() { const foo = await simpleAsync()[0]; const bar = (await simpleAsync())[0]; // ^ ^ const baz = await simpleAsync(); console.log("foo:", foo); console.log("bar:", bar); console.log("baz:", baz); } main();

However, if you want to directly get both return values, you can simply use destructuring for a multiple assignment :但是,如果您想直接获取两个返回值,您可以简单地使用解构进行多重赋值

 const simpleAsync = async function () { return ["one", "two"]; } async function main() { const [a, b] = await simpleAsync(); console.log("a:", a); console.log("b:", b); } main();

This way await is called with an actual promise, rather than a non-promise and after that is resolved, the a and b variables will be assigned.这种方式await使用实际的 promise 调用,而不是 non-promise,在解决之后,将分配ab变量。

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

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