FAQ: JavaScript Promises - Using Promise.all()

This community-built FAQ covers the “Using Promise.all()” exercise from the lesson “JavaScript Promises”.

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

Web Development

Asynchronous JavaScript

FAQs on the exercise Using Promise.all()

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!

For number 3 of this challenge, I just can’t figure out the right answer. I feel like I have it correct: Promise.all([checkSunglasses(), checkPants(), checkBags()]);. It’s saying I may have a syntax error, but I don’t think I do. Am I crazy?

1 Like

Hi,

You don’t need the () after checkPants etc. Don’t ask me why but it worked for me :slight_smile:

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

Is the onReject variable necessary to chain after .then(onFulFill) when checkAvailabilty function already has a string response when the promise is rejected? If so, why?

That did work - though I don’t know why either.

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

const onFulfill = (itemsArray) => {
  console.log(`Items checked: ${itemsArray}`);
  console.log(`Every item was available from the distributor. Placing order now.`);
};

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

// Write your code below:
const checkSunglasses = checkAvailability('sunglasses', 'Favorite Supply Co.');

const checkPants = checkAvailability('pants', 'Favorite Supply Co.');

const checkBags = checkAvailability('bags', 'Favorite Supply Co.');

Promise.all([checkSunglasses, checkPants, checkBags]).then((onFulfill) =>{
  console.log(onFulfill)
});

Can someone tell me what is wrong with my code? On Step 4. I get this error.

“Did you pass the onFulfill function into .then() as the success handler? It should be the only argument to .then() . Make sure to pass it in uninvoked.”

Don’t use the full code from the hint. It’s supposed to be similar to the hint, but not exact.

Promise.all([checkSunglasses, checkPants, checkBags]).then((onFulfill) =>{
  console.log(onFulfill)
});

If you shorten it to:

Promise.all([checkSunglasses, checkPants, checkBags]).then(onFulfill));

it should work. :crossed_fingers:

3 Likes

Thanks! But I want to know for general knowledge, why was it asking me to shorten it now?

If you leave your code like you had it, it will run when you type node app.js in the console, but the results are different than when you shorten the code to just the function call. I suggest you try it both ways after you complete the exercise to see the difference. I’m not an expert on asynchronous programming, but I believe that is the reason for the somewhat unexpected result you’ll see when you alternate between the two lines of code.

1 Like

:joy: really funny…

1 Like

I think it’s because we aren’t assigning it to a value, for example:

let checkItems = Promise.all([checkSunglasses(), checkPants(), checkBags()]);

In that case, I think we need the parentheses because we’re assigning the value of [checkSunglasses(), checkPants(), checkBags()] to checkItems. But in the exercise on codecadamy, it starts with just Promise.all(), so we don’t need the parentheses.

Hope this helps!

Where do the onFulfill and onReject come from and how can we use them if they weren’t defined in any of our programs

Having difficulty understanding whats going on with Promises.
Would help to see an explanation using foreground/background, interrupt handlers and pointers.

How I can look at the code in library.js in the terminal? Thanks.

Super late but maybe someone else finds this useful:
I think it is because checkPants etc. aren’t functions, they’re variables. So you aren’t invoking a function directly - you’re passing the variables as arguments to Promise.all. The value of the variables is the result of invoking the checkInventory() function. So you are indirectly invoking a function, but what you’re passing to Promise.all is ‘just’ a value.

4 Likes

To add on to what you said, the function has the console.log commands in it already, so if the function is called it will print to the console without needing the console.log(whileCallingTheFunction). If the function returned strings, then you would need to do the console.log while calling the function, otherwise nothing will be printed to the console.

1 Like

I agree with what you say, except this part…

I don’t think we can call the variables checkSunglasses , checkPants and checkBags themselves arguments. They are promises returned from calling checkAvailability() three times, and which are then assigned (by reference to their variable names) as individual elements to an array. It’s this array which is passed to the Promise.all() function as its single argument/parameter.

@p.ryan.dorangmail.co, @tochmy, @toskap3

This post answers your question…