在 Google Cloud Functions 中使用 process.exit

Way*_*rry 6 javascript node.js google-cloud-platform google-cloud-functions

我想知道 using 是否process.exit()是停止云函数执行的有效方法(或好主意)。它允许您编写稍微简洁的代码,因为您不必从导出的函数中显式返回。

exports.myFunction = function myFunction(req, res) {
    const abort = function(err) {
        console.error(err);
        res.status(500).send();
        process.exit(1);
    };

    doSomething(err => {
        if (err) abort(err);

        res.status(200).send("Success!");
    });
};
Run Code Online (Sandbox Code Playgroud)

小智 1

我自己也在寻找这个,在谷歌文档中找到了我的答案。本质上,您只想退出该导出函数,而退出的方式取决于它是 http 还是后台函数。

Http:函数执行时间线

exports.afterResponse = (req, res) => {
    res.end();
};
Run Code Online (Sandbox Code Playgroud)

后台:终止后台功能

exports.helloPromise = data => {
    return fetch(data.endpoint);
};
Run Code Online (Sandbox Code Playgroud)