最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

运维笔记admin8浏览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
}
发布评论

评论列表(0)

  1. 暂无评论