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

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 15,247 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,677 Points

Your quite welcome :smile:

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 15,247 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,677 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:

1 Answer

Caleb Kemp
Caleb Kemp
12,677 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.