Home > database >  Module.exports function executes before Console log - why?
Module.exports function executes before Console log - why?

Time:04-10

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?

CodePudding user response:

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.

  • Related