在Firebase Cloud Functions中使用异步/等待时返回承诺

Jaa*_*and 6 javascript async-await firebase google-cloud-functions

因此,我很高兴一直在使用async / await,因为Firebase Cloud Functions支持节点8。我正在努力做一件事情。使用可调用函数时,系统会告知您必须在函数中返回一个promise,否则它将无法正常工作。使用原始的诺言时,我很清楚如何使用它:

exports.createBankAccount = functions.region('europe-west1').https.onCall((data, context) => {
    return promiseMethod().then((result) => {
        return nextPromise(result);
    }).then((result) => {
        return result;
    }).catch((err) => {
        // handle err
    })
});
Run Code Online (Sandbox Code Playgroud)

但是现在,异步等待着,我不确定如何返回这个“承诺链”:

exports.createBankAccount = functions.region('europe-west1').https.onCall(async (data, context) => {
    const res1 = await promiseMethod();
    const res2 = await nextPromise(res1);
    return res2;
    // ??? Where to return the promise?
});
Run Code Online (Sandbox Code Playgroud)

有人知道吗

Dou*_*son 4

HTTP 函数不返回承诺。他们只是发送结果。您仍然必须正确使用 Promise 才能发送结果,但不需要返回值。HTTP 函数在发送响应时终止。请参阅文档了解更多详细信息

使用 res.redirect()、res.send() 或 res.end() 终止 HTTP 函数。

  • 好吧,您编辑了问题以显示可调用函数,而不是最初的 HTTP 函数。实际上,你会被问到一个不同的问题来代替第一个问题,这并不是 Stack Overflow 真正的工作方式。如果你完全改变问题的条款,你会让所有人都感到困惑。 (7认同)