Skip to content

Instantly share code, notes, and snippets.

@elliotec
Created June 22, 2017 00:53
Show Gist options
  • Save elliotec/b7282c3f5d6ea3c1a9af0f103ead4d32 to your computer and use it in GitHub Desktop.
Save elliotec/b7282c3f5d6ea3c1a9af0f103ead4d32 to your computer and use it in GitHub Desktop.
app.get('/characters', function (req, res) {
let mappedCharacters = [];
function mapResults(json){
mappedCharacters.push(json);
return mappedCharacters;
}
function reducePages(pages){
return pages.reduce((acc, curr) =>
acc.concat(curr), []
);
}
function getAllCharacters(pageNum){
let url = `${swapi}/people/?page=${pageNum}`;
return fetch(url)
.then(res => res.json())
.then(json => json.results)
}
const allPages = [1,2,3,4,5].map((page) => getAllCharacters(page)
.then(json => mapResults(json))
.then(pages => reducePages(pages))
);
const allCharacters = Promise.all(allPages).then(response => response);
console.log(allCharacters);
res.render('characters', {characters: JSON.stringify(allCharacters)})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment