Home > Enterprise >  Prevent node.js application from exiting once the process is done
Prevent node.js application from exiting once the process is done

Time:10-22

I wrote a really basic nodejs application which look like this:

#!/usr/bin/env node

const inquirer = require("inquirer");
const chalk = require("chalk");
const figlet = require("figlet");
const shell = require("shelljs");
const fs = require('fs');

//Defining some functions
//~~

//The main part of the code
const run = async function() {
    const answers = await form();
    //other codes
    //~~
}

run();

The main purpose of the code is to use the inquirer module to ask some question in the console, and then process the answers in the run() part. It does perfect job on that. It successfully asks the question and do what it should do with the answers.

However, The process would exit once the answer have been processed. What I want is once the answer have been processed, I want it to answer the same question again, and keep repeating that until I terminate the process manually.

I tried this:

for( ; ; ) {
    run();
}

However, It would then answer questions again and again without waiting for answers. Here is how the console looked like: console output

I want it to do these:

  1. Ask question and wait untill I enter the answer.
  2. Process the question (with the codes in run())
  3. Once it's done, go back to question 1.

How can I do this?

CodePudding user response:

Instead of doing

for( ; ; ) {
    run();
}

Do this inside run function

while (true) {
    const answers = await form();
    //other codes
    //~~
}
  • Related