Module.export

Hello!

I have a lesson about Promises

I noticed checkInventory function is being exported. As I understand, to export function it should be packed as an object. That’s why “module.exports = { checkInventory };”

const inventory = {
    sunglasses: 1900,
    pants: 1088,
    bags: 1344
};

const checkInventory = (order) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            let inStock = order.every(item => inventory[item[0]] >= item[1]);
            if (inStock) {
                resolve(`Thank you. Your order was successful.`);
            } else {
                reject(`We're sorry. Your order could not be completed because some items are sold out.`);
            }
        }, 1000);
    })
};

module.exports = { checkInventory };

And then in app.js checkInventory function is unpacked from object.

const {checkInventory} = require('./library.js');
const order = [['sunglasses', 1], ['bags', 2]];

// Write your code below:

The question is
Why do we use object, if I can simply export a function like this:

module.exports = checkInventory;

And code above works.

By what metric? Maybe you’re not making a measurement at all and therefore not seeing a difference?

If I for example made a function that adds two numbers, then I would want to be able to import that function and use it to write out the sum of two numbers to screen. That would be my measurement - showing that I’m able to invoke the code from the other module.