簡體   English   中英

Javascript異步/等待功能-添加多個返回

[英]Javascript async/await function - adding multiple returns

我有一些使用Async / Await並能正常工作的代碼...這是代碼:

async function main() {
  const x = await myfunction();
  return x;
}

我想做這樣的事情:

async function main2() {
  const x = await myfunction();
  const x2 = await myfunction2();
  return x;
  return x2;
}

我的問題是上述代碼不允許返回多於一個。

所以我的問題是...是否可以對main2()進行多次返回?

作為數組或對象返回

async function main2() {
  const x = await myfunction();
  const x2 = await myfunction2();
  return [x, x2]
  // or
  return {f1: x, f2: x2}
}

main2()
  .then(res => console.log(res))
  .catch(err => console.log(err))
return {
 x,
 x2
}

然后訪問屬性main2().xmain2().x2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM