为什么这些承诺没有解决?

Rod*_*Rod 1 javascript node.js promise async-await

在里面我需要等待promise.all吗?还有什么可能是错的?

(async => {
    const result = Promise.all(
        await domainContext.get1(id),
        await domainContext.get2(id)
    ).then(r => r);
})();
Run Code Online (Sandbox Code Playgroud)

我期待着 :

result = [get1_value, get2_value]
Run Code Online (Sandbox Code Playgroud)

我越来越 :

'{}'
Run Code Online (Sandbox Code Playgroud)

Cer*_*nce 7

Promise.all期望一个数组作为单个参数,而不是其参数中的Promise列表.此外,如果您正在使用Promise.all,请不要await在内部 - 这种目的会失败,因为那时您将传递一组已解析的值,Promise.all而不是传递一个Promises数组来等待.另外,要定义不带参数的异步函数,需要在以下后面添加一个空参数列表async:

const get1 = () => Promise.resolve('get1');
const get2 = () => Promise.resolve('get2');
(async () => {
    const result = await Promise.all([
      get1(),
      get2(),
    ]);
    console.log(result);
})();
Run Code Online (Sandbox Code Playgroud)

你也可以awaitresult数组中的每个项目,如下所示:

const get1 = () => Promise.resolve('get1');
const get2 = () => Promise.resolve('get2');
(async () => {
    const result = [
      await get1(),
      await get2(),
    ];
    console.log(result);
})();
Run Code Online (Sandbox Code Playgroud)

这可能是你想要做的,但请注意,这会导致每个项目都是串行请求,而不是并行请求.