Promise.all 返回更多值

Pjo*_*kov 2 javascript node.js es6-promise

我正在尝试使两个异步调用同时运行并等待它们完成Promise.all。如何Promise.all在 Node.js 的 es6 中使用 async/await 返回多个值?我希望我能在外面看到分配的变量。这段代码是错误的,但代表了我所需要的。

  Promise.all([
    var rowA = await buildRow(example, countries),
    var rowM = await buildRow(example, countries)
  ])
  console.log(rowA+rowB)
Run Code Online (Sandbox Code Playgroud)

有没有办法查看范围之外的这些变量?

Mar*_*nde 5

如何在 Node.js 的 es6 中使用 async/await 让 Promise.all 返回多个值?

Promise.all解析值始终是一个数组,其中每个项目都是传递给它的各个 Promise 的解析值。结果与承诺的顺序相同。

  const [rowA, rowB] = await Promise.all([
        buildRow(example, countries),
        buildRow(example, countries)
  ]);

  console.log(rowA, rowB);
Run Code Online (Sandbox Code Playgroud)

使用解构,我们分配给rowA第一个结果和rowB第二个结果。