FAQ: JavaScript Promises - Using catch() with Promises

This community-built FAQ covers the “Using catch() with Promises” 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 Using catch() with Promises

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!

prom
.then((resolvedValue) => {
console.log(resolvedValue);
})
.then(null, (rejectionReason) => {
console.log(rejectionReason);
});

This is the code example for part 8 of Javascript Promises. I was wondering why we use the ‘null’ parameter in the second .then method. Is it so that the method can return null if there is no rejection of the promise?

4 Likes

hey, i’m currently on this task and wondering why the NULL is used. did you ever find out the reason behind it ?

I’m learning myself, but i think it’s because the .then() takes two arguments - one for success and one for failure. I case of success you just need the first argument, the “resolve”-argument, but in case of failure you need the second argument, so you set the first argument to “null” since it’s not needed, only the second one, the “reject”-argument.

7 Likes

Yes would like to know why too…What’s null ding here?

also

" Remember, .then() will return a promise with the same settled value as the promise it was called on if no appropriate handler was provided."

i don’t quite get this line either…

can anybody help? Thanks!

2 Likes

Yes this makes perfect sense. You can name the first (often named onFulfilled) and second (often named onRejected) handlers for then() anything, so even if it’s named onRejected and you only have one argument for then(), it would be assumed to be the onFulfilled.

3 Likes

Remember, .then() will return a promise with the same settled value as the promise it was called on if no appropriate handler was provided.

I cant comprehend this lol I have read it more than once but i’m just not understanding this specific phrase above.

.then() will return a promise with the same settled value as the promise it was called on if no appropriate handler was provided.

??? I have tried giving the .then() method no handlers to see if it does return the same settled value the “promise” was called on … and i get nothing ? How about you? I’m stumped

So you remember when you created your first promise earlier in the lesson. The promise constructor takes a function parameter called an “executor” function. This executor function in turn takes a two function parameters referred to as the “resolve” or “reject” functions. These two functions, when called inside the executor function, can be passed “resolve” or “reject” values as strings. These are what the sentence is talking about when it refers to “will return a promise with the same settled value as the promise it was called on”. So, if you do not provide .this() with an appropriate handler, then .then() will return those “resolve” or “reject” promise values you gave them. Easy lol

6 Likes

From the lesson:

One important feature of .then() is that it always returns a promise.

Remember, .then() will return a promise with the same settled value as the promise it was called on if no appropriate handler was provided.

So .then() will return a promise even if an appropriate handler was provided. What would the promise’s settled value be in the case where a handler was provided but that handler itself didn’t return a promise?

Slight confusion here:

If the promise rejects, .then() will return a promise with the same rejection reason as the original promise and .catch()‘s failure handler will be invoked with that rejection reason.

That makes no sense to me. Shouldn’t .then() handle success while . catch() handles failure? At least that’s the impression I had before this bullet point. Going back up and reading over the text a second time, with the idea that both handle rejection, I can also invoke the idea that you were trying to tell me that .then() may be used to “return” the value that failed during the promise, and .catch() afterwards has the job of doing something with that value.
If that was what you wanted to say from the start, please slightly reword the article so that there is no confusion between what you want to explain, versus what the reader thinks you want to say based on sequence and assumption.

I understand less and less going further into this topic. I tried different approaches with the checkInventory() function. Using .then() without a succes or failure handler results in:
image

Using .then() with both a success and failure handler, results in a cleaner message:

Using .then() with a succes handler and .catch() with a failure handler, has the same outcome as using only .then() with both a success and failure handler:

So why use an additional method like .catch if the same can be achieved by using .then()?

3 Likes

What is the point of that when we can handle both in a single .then() ?

const inventory = {

sunglasses: 0,

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

can anyone explain what’s happening in **inStock ** variable and what are those things?

From my understanding of the lessons till here, I think the following is happening

  1. When a promise settles it will call its executor function.

  2. The executor function will appropriately send the success messages to the resolve or reject callbacks.
    2.1 The resolve call back take sort of success message as an argument.
    2.2 The reject callback function takes the failure message.
    I hope things are clear until this point.

  3. Now we come to the .then() function. This takes our handlers which takes care of what happens next once our promise is settled.

  4. If no handlers are defined the, normal functioning of the executor function will still happen. Which will call the resolve or reject method according to the conditions we specify.

3 Likes

The way I understand it is that if you’re using catch() then in case an error happens in your .then(), then it will be passed on and handled by the catch(), which comes next.

In contrast, if your .then() is expected to handle both cases then in case of an error it has nowhere to be passed on and remains unhandled.

This makes sense to me, at least for now… but I find these topics to be very slippery!

1 Like

I’m so confused. In part 7/11 of this section, the following is shown:

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

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

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

// Write your code below:
checkInventory(order)
	.then(handleSuccess)
	.catch(handleFailure);

Where did the “(resolvedValue)” and “(rejectReason)” come from? What are they?

why do we use promise? I can’t understand the use of promise? can anyone explain with example?

meaning of asynchronous programming in simple language?