FAQ: JavaScript Promises - The onFulfilled and onRejected Functions

module is the current module.
exports is an object within the current module.

module.exports = { var1 } // You are assigning a object with the item var1 to the exports object
module.exports = var1 // You are assigning var1 to the exports object

Both work.

Proof:
If you go to library.js, and type:
console.log(module.exports);
And in bash you type
node library.js // { checkInventory: [Function: checkInventory] }

You will get the same without squiggly brackets if you remove the ‘object squiggly brackets’

@mtf

Yes, I can see now that using the curly braces places the function checkInventory within an object as a key/value pair, and it is this object that we are then exporting.

When the curly braces are removed, what we are exporting is just the function, as follows:

module.exports = checkInventory;
console.log(module.exports);

// => [Function: checkInventory] i.e. not a property (key/value pair) of an object 

I assume that this is what you meant.

When importing a single function using Node.js notation… e.g.

const {checkInventory} = require('./library.js');

… wrapping the variable name in curly braces when declaring it with const  looked strange to me. When declaring a variable, the only other time I’d seen the variable name wrapped in curly braces like this, is when assigning an object property (i.e. a key/value pair) to a separate variable using destructured assignment.
So, I think that if we…
(i) assign one or more pieces of data to an object as key/value pairs by wrapping their variable names (separated by commas) in curly braces, and then…
(ii) export this object to another file…
… on importing this object, if we use the above code to assign its key/value pairs to separate variables again, we must be doing it using destructured assignment. Do you agree?

To be able to export/import more than one separate piece of data, I can see that we have to use curly braces to assign all of them to a single object as its properties on export, and also to then assign each property to a separate variable again on importing. However, if we are only exporting one piece of data from our file (such as with the  checkInventory  function in our example), then I don’t think we need to use curly braces in either the export or import statement, because we can just (i) export the data as a single variable without having to first assign it to an object, and then (ii) reassign the data directly to a variable in the file we are importing to, without having to use destructured assignment; although, I can also see that there is nothing stopping us from assigning just one piece of data to an object by wrapping it in curly braces in the export statement, but if we then don’t use curly braces in the import statement as well, we would have to access the data as a property of an object by using dot notation, rather than being able to access it directly via a single variable name e.g.

// export file
const data = 'myData';
module.exports = {data};

// import file
const data = require('./file.js');

console.log(data);           // => {data: [Function: data]}
console.log(data.data);      // =>        [Function: data]

Even though the lesson Intermediate Javascript Modules only covers exporting/importing separately named exports with ES6 notation, as follows…

// export file
export {identifier1, identifier2, identifier3 ... };

// import file
import {identifier1, identifier2, identifier3 ... } from './file'; 

… after what I’ve now found out, I’m going to assume that we can do the same thing, and wrap our variable names in curly braces, with Node.js notation, as follows…

// export file
module.exports = {identifier1, identifier2, identifier3 ... };

// import file
const {identifier1, identifier2, identifier3 ... } = require('./file.js'); 
2 Likes

It is what makes the most sense, so yes.

1 Like