在进程退出之前运行异步函数

jwe*_*rre 4 process event-handling node.js

我有一个用 Node.js 编写的简单 shell 脚本,我试图在进程退出之前进行一些清理 (\xe2\x8c\x83C)。我已经编写了以下代码,但由于某种原因清理功能未完成。有什么想法吗?

\n\n
if (keepAlive) {\n\n   process.stdin.resume();\n\n    [\'exit\', \'SIGINT\'].forEach(function(signal) {\n\n        process.on(signal, function() {\n\n        console.log("Received Signal: \'" + signal + "\'. Cleaning up...");\n\n        cleanup(function(err) {\n          console.log(\'this function is called but does not finish\')\n          process.exit();\n        });\n\n      });\n\n    });\nelse {\n    process.exit();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在使用 Node v6.9.2

\n

小智 6

你可以这样做 :

process.on("SIGTERM", () => {
    ayncFunction().finally(() => process.exit(0));
});

process.on("SIGINT", () => {
    ayncFunction().finally(() => process.exit(0));
});

process.on("exit", code => {
    console.log(`APP stopped !`);
});
Run Code Online (Sandbox Code Playgroud)