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 Exploring Async/Await Convert Promise Handling to Async/Await

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

returns promise

If promise.all() also returns a Promise and having it inside an Async function, then the returned valued shouldn't be Promise inside Promise?

Syntax below

async function getPeopleInSpace (url) {
  const peopleResponse = await fetch(url);
  const peopleJSON = await peopleResponse.json();

  // map over the people array in peopleJSON object
  const profiles = peopleJSON.people.map (async (person) => {
        const craft = person.craft;
        const profileResponse = await fetch(wikiUrl +                                                       person.name);
        const profileJSON = await profileResponse.json();

        return {...profileJSON, craft};
    });
//console.log( Promise.all(profiles));
  return Promise.all(profiles);
}
Caleb Kemp
Caleb Kemp
12,754 Points

Your quite welcome :smile:

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
karan Badhwar
Web Development Techdegree Graduate 18,135 Points

hey Caleb Kemp , I was just logging some code on console, but it returns a promise if i log Promise.all

const all = Promise.all([
  fetchData('https://dog.ceo/api/breeds/image/random'),
  fetchData('https://dog.ceo/api/breeds/list')

]);
console.log(all);

OUTPUT

Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Array(2)
0: {message: 'https://images.dog.ceo/breeds/chihuahua/n02085620_7440.jpg', status: 'success'}
1: {message: Array(95), status: 'success'}
length: 2
[[Prototype]]: Array(0)

Am not sure what to follow on this and on a video I will post the link the below, He(Guil Hernandez) said, it returns one single Promise https://teamtreehouse.com/library/manage-multiple-requests-with-promiseall#transcript @1:43

Caleb Kemp
Caleb Kemp
12,754 Points

Ok, so the object itself will be a promise (which could be important if you need to do type checks or something), but the value (what is returned) of the promise will be an array. You can see this in the log you printed

[[PromiseResult]]: Array(2)
0: {message: 'https://images.dog.ceo/breeds/chihuahua/n02085620_7440.jpg', status: 'success'}
1: {message: Array(95), status: 'success'}
length: 2

also if you reference the link I sent you earlier and go to 1:32 where he prints the results of promise.all(). The object is of type "promise", but the returned value is an array of 6 promise objects. Glad to see you making progress :grinning:

2 Answers

Caleb Kemp
Caleb Kemp
12,754 Points

I see where your coming from, but promise.all() does not return a promise object. promise.all() is a promise method that returns an array of promise objects. Here is a link to the video that covers it. It mentions what the return value is around 0:58. Great to see you steadily working away at the lessons. Hope this helped.