Skip to content

Instantly share code, notes, and snippets.

@herrkessler
Created August 15, 2017 11:12
Show Gist options
  • Save herrkessler/495bcfa34c48b2e258344299b2f126e1 to your computer and use it in GitHub Desktop.
Save herrkessler/495bcfa34c48b2e258344299b2f126e1 to your computer and use it in GitHub Desktop.
simple node async / await multiple requests with axios
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