Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda