Created
June 22, 2017 00:53
-
-
Save elliotec/b7282c3f5d6ea3c1a9af0f103ead4d32 to your computer and use it in GitHub Desktop.
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
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