Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
How can I prompts loop in Node.js?

I want the main.js loop to be re-selectable when the user has selected option.

Here is the code.

main.js

const prompts = require('prompts');
var option = require('./option.js');

(async () => {
  const response = await prompts({

    type: 'select',
    name: 'value',
    message: 'choice',
    
    choices: [
      { title: 'option1', description: 'option1 description', value: '1' },
     
      { title: 'exit',  description: 'exit from script',value: '0' }

    ],
    initial: 1

    
  }
  
  
  
  ).then(response => {


if(response.value == 1){


  console.info('you select option1');
option.option1()

// i want to run main.js again here
}
else {
  console.info('you select exit');
// i want to exit from main.js 
}
  });

})();

option.js

module.exports = {


    option1:function() {
        console.log("You have selected option 1")
    }
}

When the user has selected option1, after the option.js script's function is done, I would like it to return to the new options page.

I've tried many ways but I still can't find a way. What can I try?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Is this the solution to your problem? Also notice when using await, dont use then.

const prompts = require('prompts');
var option = require('./option.js');


async function prompt () {
 
  const response = await prompts({

    type: 'select',
    name: 'value',
    message: 'choice',
    
    choices: [
      { title: 'option1', description: 'option1 description', value: '1' },
     
      { title: 'exit',  description: 'exit from script',value: '0' }

    ],
    initial: 1
  })
  
  return response.value === '1'?option.option1() & prompt():false
}
prompt()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!