Last active
May 14, 2021 22:32
-
-
Save bcalmac/d3d58cca33e464b2e05009ebb4f0d780 to your computer and use it in GitHub Desktop.
Axios with concurrent requests
This file contains 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
// What films does planet Tatooine shows up in? | |
// http://swapi.dev/api/planets/1/ | |
// Functional-style friendly functions | |
function get(url) { | |
return axios.get(url).then(r => r.data) | |
} | |
function getAll(urls) { | |
return Promise.all(urls.map(get)) | |
} | |
function print(object) { | |
console.log(object) | |
} | |
async function filmsOfTatooine() { | |
const planet = await get('http://swapi.dev/api/planets/1/') | |
const films = await getAll(planet.films) | |
films.map(r => r.title).forEach(print) | |
} | |
async function main() { | |
try { | |
await filmsOfTatooine() | |
} catch (e) { | |
console.error("SWAPI invocation failed:", e) | |
} | |
} | |
// noinspection JSIgnoredPromiseFromCall | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment