Skip to content

Instantly share code, notes, and snippets.

@knowthen
Created April 29, 2016 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knowthen/d29e416f19bc64e785561e61166c062e to your computer and use it in GitHub Desktop.
Save knowthen/d29e416f19bc64e785561e61166c062e to your computer and use it in GitHub Desktop.
var Promise = require('bluebird');
var request = require('request');
/**
* Find the temperature at a location
* @param {String} location
* @param {function} callBack
* @return {Promise}
*/
var getTemperatureByLatLong = function(latitude, longitude, callBack){
var temp;
var def = Promise.defer();
var APIKEY = 'YOURAPIKEYHERE';
var url = 'http://api.openweathermap.org/data/2.5/weather?appid=' + APIKEY + '&units=imperial&lat=' + latitude + '&lon=' + longitude;
console.log(url);
var options = {
url: url,
json: true
};
request(options, function(err, response, body){
if(err){
if(callBack){
callBack(err);
}
def.reject(err);
}
else{
if(body && body.main && body.main.temp){
temp = body.main.temp;
}
if(callBack){
callBack(null, temp);
}
def.resolve(temp);
}
});
return def.promise;
}
module.exports = {
getTemperatureByLatLong: getTemperatureByLatLong
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment