Created
July 17, 2019 16:49
-
-
Save fabriciofmsilva/aeb4700030b26ba423f5bbc9984de189 to your computer and use it in GitHub Desktop.
Async JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fetch("http://localhost:3000/people") | |
// .then(response => response.json()) | |
// .then(people => { | |
// return people.map(person => { | |
// return fetch("http://localhost:3000/person" + person.id + "/address") | |
// .then(response => response.json()) | |
// .then(address => { | |
// person.address = address; | |
// return person; | |
// }); | |
// }); | |
// }) | |
// .then(people => { | |
// Promise.all(people).then(result => { | |
// console.log(result); | |
// }); | |
// }); | |
const URL = 'https://swapi.co/api/'; | |
fetch(`${URL}people`) | |
.then(response => response.json()) | |
.then(people => { | |
return people.results.map(person => { | |
return fetch(person.homeworld) | |
.then(response => response.json()) | |
.then(homeworld => { | |
person.homeworld = homeworld; | |
return person; | |
}); | |
}); | |
}) | |
.then(people => { | |
Promise.all(people).then(result => { | |
console.log(result); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment