【问题标题】:Take multiple Arrow functions as Promise and print the values of all resolves from the Arrow functions将多个箭头函数作为 Promise 并从箭头函数打印所有解析的值
【发布时间】:2020-06-28 09:46:21
【问题描述】:

我正在尝试打印

[ '第一次通话!','第二次通话!' ]

我的代码:

function get(apiCalls) {
    return Promise.all(apiCalls)
}

//------------ START
let promise = get([
    () => Promise.resolve("First call!"),
    () => Promise.resolve("Second call!")
]);

if (promise) {
    promise.then((result) => result).catch((err) => console.log(err));
}
//------------ END

但我得到了输出:

[[函数],[函数]]

STARTEND的代码sn-p不能改

【问题讨论】:

    标签: javascript asynchronous ecmascript-6 promise arrow-functions


    【解决方案1】:

    您将一组函数传递给Promise.all,但它需要一组承诺。调用函数:

    return Promise.all(apiCalls.map(f => f()))
    

    或通过承诺:

    let promise = get([
        Promise.resolve("First call!"),
        Promise.resolve("Second call!")
    ]);
    

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2016-03-24
      • 2021-06-24
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多