FAQ: JavaScript Promises - The onFulfilled and onRejected Functions


#1

This community-built FAQ covers the “The onFulfilled and onRejected Functions” exercise from the lesson “JavaScript Promises”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Asynchronous JavaScript
Asynchronous JavaScript

FAQs on the exercise The onFulfilled and onRejected Functions

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#3

I can’t type anything in the terminal! (Which is required for step 5.)


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

const order = [['sunglasses', 1], ['bags', 2]];

// Write your code below:
const handleSuccess = (resolvedValue) => {
    console.log(resolvedValue);
};

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

checkInventory(order)
    .then(handleSuccess, handleFailure);
library.js
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 };```

> why  in this(let inStock = order.every(item => inventory[item[0]] >= item[1]);)do you append 'inventory' ?

#5

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


I couldn’t figure out what’s going up there.

  1. first, what’s the use of this expression ‘inventory[item[0]] >= item[1])’. This seems like an array but ‘inventory’ is an object.
  2. what kind of function declaration is this? Is this a loop?
  3. And what’s the use of a success handler and a failure handler if all they do is; to log or return resolve() a reject(). This can be even accomplished by passing the executer function into the promise.