Firebase云功能和异步/等待

时间:2019-07-14 15:46:09

标签: javascript firebase async-await google-cloud-functions

我正在Firestore的Firebase Cloud Functions上使用async / await,因为似乎受到了支持(也许不是正式的支持?)。

但是,我注意到在函数结束之前某些代码没有被执行。我总是在日志中看到某个函数已完成,几分钟后,我看到了函数一部分的日志。

我无法分享我的函数的确切代码,但是看起来像这样(我编辑了一些部分):

exports.newMessages = functions
    .region('europe-west1')
    .firestore
    .document('/REDACTED/{id1}/REDACTED/{id2}')
    .onCreate(async (snap, context) => {

        // some code removed

        // get the a list of documents
        const someDocs = await someDocuments(context.params.id, context.params.id2);

        someDocs.forEach(async snap => {
            let uid = snap.data().uid;

            if (someCondition) {
                await quickOperation(context.params.id, context.params.id2, uid);
                await sendPushNotification(uid);
            }
        })

        await anotherOperation();
    });

quickOperationanotherOperation中的日志出现在Function execution took ...日志行之前。但是sendPushNotification中的日志显示在该日志行之后,大约两分钟后。

sendPushNotification中的代码在Firestore中执行一系列读取,然后执行推送通知。即使在运行云功能两分钟后,也会发生这种情况。

  • 我正在使用异步/等待这一事实归咎于我吗?
  • 将以任何方式重构和使用TypeScript(它还支持Async / Await)帮助吗?

0 个答案:

没有答案
相关问题