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

163
Visualizações
Readline infinitely while handling async console logs in Node.js

I'm trying to create a script where you can write commands:

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
})

let askCommands = async () => {
  readline.question('> ', async (command) => {
    // If user exits close readline
    if(command === 'exit') {
      readline.close();
      return process.exit();
    }
    switchCommands(command);
    askCommands();
  })
}

let switchCommands = (command) => {
  if(command === 'test') {
    setTimeout(() => {
      console.log('Hello!')
    }, 1000)
  } else {
    console.log(`Invalid command '${command}'`)
  }
}

askCommands();

It works fine until I use an async console.log(). This is because the script writes the async outputs without removing the '> ' and then generates a new line that does not have the '> '. Moreover, if in this new line you write any character and then delete it the '> ' is added automatically.

I would like to write async logs without having the '> ' before them and without removing the '> ' where users write the commands. Is there any easy solution to this problem?

about 4 years ago · Juan Pablo Isaza
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