Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Blake Larson
Blake Larson
13,014 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'

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