在firebase云功能中返回多个异步函数的承诺?

Dan*_*ama 5 javascript firebase google-cloud-functions google-cloud-firestore

所以我有一个Firebase云功能,可以调用2个异步功能.

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

asyncFunction1和asyncFunction2都返回一个promise.

现在,Firebase决定我们应该这样做

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

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

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

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

有任何想法吗?提前致谢.

Zee*_*tti 11

你可以试试Promise.all([asyncFunction1(), asyncFunction2()]).看看链接

  • 他传递的参数没有方括号. (2认同)