在自己的回调中调用一个函数
我的NodeJS应用程序运行我的C ++应用程序并观察它。如果应用程序被杀死,服务器将再次运行它。如果我的应用程序运行了几天,如果假设这个kill / die场景发生了太多次,它会导致堆栈溢出吗?如果是,请您提供解决方案吗?
谢谢
import { execFile } from "child_process";
function runRedirector(){
execFile("./redirector.out", ["1"], {}, function(error, stdout, stderr) {
runRedirector();
});
}
回答如下:
由于execFile
的异步性质,你调用堆栈不会增长。当调用回调时,外部调用已经从调用堆中跳出
const {execFile} = require("child_process");
let i = 0
function runRedirector(){
execFile("./redirector.out", ["1"], {}, function(error, stdout, stderr) {
console.log('In callback', i++)
runRedirector();
});
console.log('In runDirector', i); // this will be logged first
}