Created
August 15, 2017 11:12
-
-
Save herrkessler/495bcfa34c48b2e258344299b2f126e1 to your computer and use it in GitHub Desktop.
simple node async / await multiple requests with axios
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
const axios = require('axios'); | |
const locations = ['London', 'San Francisco', 'Tokyo', 'Berlin']; | |
const apiURL = 'http://api.openweathermap.org/data/2.5/weather'; | |
const token = 'xxxxx'; | |
const logPosts = async () => { | |
try { | |
let allLocations = locations.map(town => axios(`${apiURL}?q=${town}&APPID=${token}`)); | |
let weather = await Promise.all(allLocations); | |
weather.forEach(location => { | |
console.log(location.data); | |
}); | |
} catch (error) { | |
console.error('Error:', error); | |
} | |
}; | |
logPosts(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment