nodejs runs a program in a continuous loop

You can just remove the rl.close() and call yourself again to keep asking for more input:

function printforme() {
    rl.question("message: ", function(answer) {
        console.log("Your message: ", answer);
        // check if we're supposed to exit
        if (answer === "exit") {
        } else {
            // ask for input again

When you call rl.close(), there is nothing left for the nodejs app to do so it exits. If you want to ask another question, then remove the rl.close() and call printforme() again from inside the rl.question() callback. Because the callback is asynchronous, this will not create stack buildup. The stack completely unwinds before the async callback is called.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top