Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda