Continue execution after sending response (Cloud Functions for Firebase)

前端 未结 3 690
醉话见心
醉话见心 2020-11-27 21:56

I\'m using Firebase Functions with https triggers, and I was wondering how long after sending the response to the client, the functions keeps executing. I want to send a res

3条回答
  •  臣服心动
    2020-11-27 22:21

    If the expected response is not pegged to the outcome of the execution, then you can use

    module.exports.doSomeJob = functions.https.onRequest((req, res) => {
    res.write('SUCCESS')
    return doSomeAsyncJob()
    .then(() => {
        emailSender.sendEmail();
    })
    .then(() => {
        res.end();
    })
    .catch(...);
    });
    

    This sends back a response a soon as a request is received, but keeps the function running until res.end() is called Your client can end the connection as soon as a response is received back, but the cloud function will keep running in the background Not sure if this helps, but it might be a workaround where the client needs a response within a very limited time, considering that executing pub/sub requires some extra processing on its own and takes time to execute

提交回复
热议问题