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.

5 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

2 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?