【问题标题】:Returning a promise of multiple async functions in a firebase cloud function?在 Firebase 云函数中返回多个异步函数的承诺?
【发布时间】:2018-08-10 16:16:57
【问题描述】:

所以我有一个调用 2 个异步函数的 Firebase 云函数。

exports.someFunction = functions.firestore
  .document('some/path')
  .onCreate(event => {
    asyncFunction1();
    asyncFunction2();
  });

asyncFunction1 和 asyncFunction2 都返回一个 Promise。

现在,Firebase dictates 我们应该这样做

通过返回 JavaScript 承诺来解析执行异步处理的函数(也称为“后台函数”)。

但是,由于我的函数正在执行两个异步进程,我应该返回什么?我试过做

exports.someFunction = functions.firestore
  .document('some/path')
  .onCreate(event => {
    return Promise.all(
      asyncFunction1(),
      asyncFunction2()
    );
  });

这可行:两个函数都被正确调用和执行,但在调用云函数时我也收到错误TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined at Function.all。

有什么想法吗?提前致谢。

【问题讨论】:

    标签: javascript firebase google-cloud-functions google-cloud-firestore


    【解决方案1】:

    你可以试试Promise.all([asyncFunction1(), asyncFunction2()])。看link

    【讨论】:

    • 他传递的参数没有方括号。
    • @MathiasLykkegaardLorenzen 魔鬼在细节中,:) Promise.all 期望一系列承诺,而不是参数中的承诺列表。
    猜你喜欢
    • 2019-12-17
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2020-01-11
    • 1970-01-01
    • 2019-01-15
    相关资源
    最近更新 更多