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

Why to we need to invoke .then() method, when we can get the same result from this code:

 let prom = new Promise((resolve, reject) => {
  let num = Math.random();
  if (num < .5 ){
    resolve('Yay!');
  } else {
    reject('Ohhh noooo!');
  }
});

const handleSuccess = (resolvedValue) => {
  console.log(resolvedValue);
};

const handleFailure = (rejectionReason) => {
  console.log(rejectionReason);
};

prom.then(handleSuccess, handleFailure);
console.log(prom);

I don’t understand why we need the .then() method with a success and failure handler if the promise the method is call upon already contains a return value for resolve and reject. It all seems a bit overdone to me. Can you expand a bit further on the necessity of the .then() method?

1 Like

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);

})

};

why we are using setTimeout() here. or why we want our function to be asynchronously.

I was wondering why we need the handleFailure() handleSuccess() functions? It seems like a lot of extra code. Why can’t we just wrap resolve() and reject() with console.log()?

Thanks!

let inStock = order.every(item => inventory[item[0]] >= item[1]);

can any one explain the logic of this code

The above callback is a predicate function that loops through the order array and checks the inventory, returning true if in stock, and false if out of stock. As the interator implies, every item must be in stock for inStock to be set to true, else it will be set to false.

1 Like

for those, like me, who didn’t notice: there are 2 tabs!

I wrote the right code in the wrong tab, as I didn’t know I should use 2 different tabs.

Hello there, pardon my stupid question but can anyone explain what is the difference in the expressions below:

checkInventory(order)
.then(handleSuccess, handleFailure);

checkInventory(order).then(handleSuccess, handleFailure);

Why does the first one work and the second does not?

That’s odd, they should both work, with or without the white space.

Unable to pass 2.

const handleSuccess = (resolvedValue) => {

console.log(resolvedValue);

};