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

294
Visualizações
Module.exports function executes before Console log - why?

I have read a few answers to similar questions, but haven't been able to understand why my application behaves this way.

I am updating a CLI application to be modular. The main script imports a function called questionOne after a console.log:

const questionOne = require('./modules/questionOne');

console.log('\n');
console.log('Which DB do you want to use?'.inverse);

questionOne();

questionOne uses readline-sync to ask a question and execute code depending on the answer. But, when I run my application... the question is asked first, and then Which DB do you want to use runs after the user has asked the question. Why?

For Reference

The code for questionOne:

const colors = require('colors');
const readlineSync = require('readline-sync');

//Data Modules
const { dbList } = require('../data/dbList.json');

const db = readlineSync.keyInSelect(dbList);

//people = 0 || offers = 1 || networks = 2

const questionOne = () => {
    if (db === 0) {
        console.log('\n');
        console.log('Which Collection would you like to use?'.inverse.bold);
        // const colRef = readlineSync.keyInSelect();
        // dbPeople(colRef);
    } else if (db === 1) {
        console.log('You picked offers');
    } else if (db === 2) {
        console.log('You picked networks');
    } else {
        process.exit();
    }
};

module.exports = questionOne;

I understand that I can put the console.log inside the module. But I am curious why javascript behaves this way?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you first to require('./modules/questionOne');, that runs all the code at the top level of the module you are loading. And, it runs that code synchronously, meaning it doesn't return until that top level code returns.

So, in your example, that's going to run all the code at the top level of your questionOne module. That top level code includes this line:

const db = readlineSync.keyInSelect(dbList);

So, that line of code will run before the module has finished loading and before it returns from this:

const questionOne = require('./modules/questionOne');

So, that should explain why the first thing that happens is that you get the prompt from const db = readlineSync.keyInSelect(dbList);. If you want to display something before that, then put that before it in the code.

Then, after that promise is done, the only other things that happen in your questionOne module are the definition of the questionOne function and the assignment module.exports = questionOne;. At that point, the require('./modules/questionOne'); returns and the value from the module.exports is assigned to your questionOne variable in your main module.

Then, and only then, the rest of your main module runs and it executes:

console.log('\n');
console.log('Which DB do you want to use?'.inverse);

questionOne();

I understand that I can put the console.log inside the module. But I am curious why javascript behaves this way?

It's just executing code in the order encountered. Nothing here appears asynchronous so it's just simple run one line of code after the other as they are encountered.

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