Asynchronous https firebase functions

后端 未结 4 1013
一整个雨季
一整个雨季 2021-01-15 06:15

Should HTTPS functions return asynchronous promises like realtime functions have to? We haven\'t been returning in HTTPS functions (just using res.status.send etc), and it l

相关标签:
4条回答
  • 2021-01-15 06:29

    After much looking around , this is implementation with a Promise worked for me to return a value from a Google Cloud Function where the function needs to make a third-party asynchronous call :

    exports.getSomeAccessToken = functions.https.onCall((data, context) => {
    
        var dataStr = JSON.stringify(data, null, '\t');   
        console.log('ENTER [getSomeAccessToken], got dataStr:  ' + dataStr);
    
        return new Promise((resolve, reject) => {
    
            gateway.clientToken.generate({}, function (err, gatewayResponse) {
    
                var result = {
                    clientToken: gatewayResponse.clientToken
                };
    
                var resultStr = JSON.stringify(result, null, '\t');
                console.log("resultStr : " + resultStr);
    
                resolve(result);
            });
        });
    
    });
    
    0 讨论(0)
  • 2021-01-15 06:32

    Your cloud functions should return"end" with either of the following

    res.redirect(), res.send(), or res.end()

    What they mean by returning promises, is lets imagine you have a cloud function that updated a node in your realtime database, you would like to complete that work before responding to the HTTP request.

    Example code

    let RemoveSomething = functions.https.onRequest((req, res) => {
        cors(req, res, () => {
            // Remove something
            DoDatabaseWork()
                .then(function (result) {
                    res.status(200).send();
                })
                .catch(function (err) {
                    console.error(err);
                    res.status(501).send();
                });
        });
    });
    

    Update: Added DoDatabaseWork example.

    const DoDatabaseWork = function () {
        return new Promise(function (resolve, reject) {
            // Remove SomeNode
            admin.database().ref('/someNode/').remove()
                .then(function (result) {
                    resolve();
                })
                .catch(function (err) {
                    console.error(err);
                    reject();
                });
        });
    }
    
    0 讨论(0)
  • 2021-01-15 06:47

    This works now in the latest Firebase:

    exports.asyncFunction = functions.https.onRequest(async (request, response) => {
        const result = await someAsyncFunction();
        response.send(result);
    });
    
    0 讨论(0)
  • 2021-01-15 06:50

    HTTP functions currently do not respect returned promises - they require a sent result in order to terminate normally. If an HTTP function doesn't send a result, it will time out.

    All other types of functions require a returned promise in order to wait for asynchronous work to fully complete.

    If you don't have any async work to wait for, you can just return immediately.

    These are the three cases outlined in the docs.

    0 讨论(0)
提交回复
热议问题