Mustafa Chelik
Mustafa Chelik

Reputation: 2184

Call a function inside its own callback

My NodeJS application runs my C++ application and watches it. 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? 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();
    });
}

Upvotes: 1

Views: 50

Answers (1)

Yury Tarabanko
Yury Tarabanko

Reputation: 45106

You call stack would not grow due to async nature of 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
}

Upvotes: 3

Related Questions