Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Asynchronous Programming with JavaScript Understanding Promises Handle Multiple Promises with Promise.all

Promise.all

i honestly didnt understand how promise.all works!!I understand the definitions but not the code associated with it ,I really need help here!

2 Answers

I am having a problem with Promise all because the wiki api is not finding one of the astronauts. How can I handle this?

Kirby Lockard
Kirby Lockard
8,026 Points

I'm having the same issue. It makes sense why it doesn't work, because the Promises.all seems to return only one resolve or reject for the whole array, and because one astronaut doesn't have a wiki page it returns a 404 and it rejects the whole array... there has to be a different way to create an array to iterate over to generate the HTML for the astronauts with a wiki page and then skip the one without a wiki. But I'm not comfortable enough with whats going on to be able to restructure it myself yet unfortunately. Some help would be much appreciated for sure.

Kirby Lockard
Kirby Lockard
8,026 Points

Found a solution in another question here

Blake Larson
Blake Larson
12,988 Points

Promise.all takes an array of promises and resolves as soon as all of them resolve individually.

When all promises resolve you then do something...

const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
});
// expected output: Array [3, 42, "foo"]

When one promise rejects then Promise.all doesn't resolve.

const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
  setTimeout(reject, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
}).catch(function(err) {
  console.log('Error'); // some coding error in handling happened
});
// expected output: 'Error'