简体   繁体   English

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

[英]Call a function inside its own callback

My NodeJS application runs my C++ application and watches it. 我的NodeJS应用程序运行我的C ++应用程序并观察它。 If application gets killed, server runs it again. 如果应用程序被杀死,服务器将再次运行它。 If my application would run for days, will it cause stack overflow if hypothetically this kill/die scenario happen too many times? 如果我的应用程序运行了几天,如果假设这个kill / die场景发生了太多次,它会导致堆栈溢出吗? If yes, can you please provide a solution? 如果是,请您提供解决方案吗?

Thank you 谢谢

import { execFile } from "child_process";

function runRedirector(){
    execFile("./redirector.out", ["1"], {}, function(error, stdout, stderr) {
    runRedirector();
    });
}

You call stack would not grow due to async nature of execFile . 由于execFile异步性质,您调用堆栈不会增长。 By the time the callback got called the outer call would already be poped out of the callstack 当调用回调时,外部调用已经从调用堆中跳出

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM