Skip to content

Instantly share code, notes, and snippets.

@Rchristiani
Last active November 17, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rchristiani/2b1647aeb0bb4b8cce68fde89825ea4d to your computer and use it in GitHub Desktop.
Save Rchristiani/2b1647aeb0bb4b8cce68fde89825ea4d to your computer and use it in GitHub Desktop.
//Function to get all the characters from the passed data
const getCharacters = (charArray) => {
//With that array of data map it and return a fetch for each of them
return charArray.map(url => fetch(url).then(res => res.json()))
}
//Call all the films
fetch(`http://swapi.co/api/films/`)
//Convert response to json
.then(res => res.json())
//Get the results
.then(films => {
//For each of the films get that one film
films.results.forEach(film => {
//Pass the array of characters to the getCharacters function
//Using Promise.all wait until it is all done
Promise.all(getCharacters(film.characters))
//Get that characters back
.then(res => {
//Make some containers
const $container = $('<section>');
const $movieTitle = $('<h2>').text(film.title);
const $characterContainer = $('<ul>');
//iterate over the characters
res.forEach(char => {
//Create li for each
$characterContainer.append(`<li>${char.name}</li>`);
});
//Append to container
$container.append($movieTitle,$characterContainer);
//Append to main
$('main').append($container);
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment