Skip to content

Instantly share code, notes, and snippets.

@fabriciofmsilva
Created July 17, 2019 16:49
Show Gist options
  • Save fabriciofmsilva/aeb4700030b26ba423f5bbc9984de189 to your computer and use it in GitHub Desktop.
Save fabriciofmsilva/aeb4700030b26ba423f5bbc9984de189 to your computer and use it in GitHub Desktop.
Async JS
// 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