Skip to content

Instantly share code, notes, and snippets.

@cianclarke
Created December 16, 2016 09:11
Show Gist options
  • Save cianclarke/f959a2d3f4d048e6a29988ccbf888661 to your computer and use it in GitHub Desktop.
Save cianclarke/f959a2d3f4d048e6a29988ccbf888661 to your computer and use it in GitHub Desktop.
var async = require('async');
var request = require('request');
async.map({
dubai : "Dubai",
dublin : "Dublin",
paris : "Paris",
rome : "Rome"
}, function(city, callback){
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=' + city,
json : true
}, function(err, response, weatherBody){
callback(null, weatherBody.main.temp);
});
}, function(err, weatherResult){
console.log(weatherResult);
});
async.parallel({
dubai: function(callback) {
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=Dubai',
json : true
}, function(err, response, weatherBody){
callback(null, weatherBody.main.temp);
});
},
dublin: function(callback){
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=Dublin',
json : true
}, function(err, response, weatherBody){
callback(null, weatherBody.main.temp);
});
},
paris: function(callback){
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=Paris',
json : true
}, function(err, response, weatherBody){
callback(null, weatherBody.main.temp);
});
},
rome: function(callback){
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=Rome',
json : true
}, function(err, response, weatherBody){
callback(null, weatherBody.main.temp);
});
},
}, function(err, results) {
if (err){
console.error('stuff went wrong: ' + err);
return;
}
console.log(results);
});
async.waterfall([
function(callback){
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=Dublin',
json : true
}, function(err, response, weatherBody){
callback(null, weatherBody.main.temp);
});
},
function(dublinWeather, callback){
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=Dubai',
json : true
}, function(err, response, weatherBody){
var dubaiWeather = weatherBody.main.temp;
callback(null, dubaiWeather + dublinWeather);
});
},
function(dubaiAndDublinWeather, callback){
request({
url : 'http://api.openweathermap.org/data/2.5/weather?appid=035957dcae9879b8e7c72469f4811705&q=Rome',
json : true
}, function(err, response, weatherBody){
var romeWeather = weatherBody.main.temp;
callback(null, dubaiAndDublinWeather + romeWeather);
});
}
], function(err, dubaiAndDublinAndRomeWeather){
console.log(dubaiAndDublinAndRomeWeather);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment