当前位置: 代码迷 >> JavaScript >> 在自己的回调中调用一个函数
  详细解决方案

在自己的回调中调用一个函数

热度:74   发布时间:2023-06-13 12:22:36.0

我的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
}
  相关解决方案