Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Last active May 14, 2021 22:32
Show Gist options
  • Save bcalmac/d3d58cca33e464b2e05009ebb4f0d780 to your computer and use it in GitHub Desktop.
Save bcalmac/d3d58cca33e464b2e05009ebb4f0d780 to your computer and use it in GitHub Desktop.
Axios with concurrent requests
// 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