PirateApp PirateApp - 1 month ago 13
Javascript Question

How to avoid code duplication in JavaScript in the attached snippet?

I am trying to find the Weather using

Open Weather
map and I have 2 methods,
findWeatherByLocation
and
findWeatherByCity
. I am assuming that
JavaScript
doesn't support
method overloading
and hence the 2 different names. Both methods accept a
callback
function that will be triggered and does the same thing.

function findWeatherForCity(senderID, city, countryCode, callback) {
//Lets configure and request
request({
url: constants.OPEN_WEATHER_MAP_BASE_URL, //URL to hit
qs: {
q: city + ',' + countryCode,
appid: constants.OPEN_WEATHER_MAP_API_KEY
}, //Query string data
method: 'GET', //Specify the method
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
let weather = getWeatherReport(JSON.parse(body));
callback(weather ? weather : null);
}
else {
console.error(response.error);
callback(null);
}
});
}

/*
lat, lon coordinates of the location of your interest
* http://openweathermap.org/current
*/

function findWeatherForLocation(senderID, location, callback) {
//Lets configure and request
request({
url: constants.OPEN_WEATHER_MAP_BASE_URL, //URL to hit
qs: {
lat: location.lat,
lon: location.lon,
appid: constants.OPEN_WEATHER_MAP_API_KEY
}, //Query string data
method: 'GET', //Specify the method
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
let report = getWeatherReport(JSON.parse(body));
callback(report ? report : null);
}
else {
console.error(response.error)
callback(null);
}
});
}


As you can see, the
function(error, response, body)
does the same thing in both places. If I make a separate
function(error, response, body)
which is common for both
findWeatherByCity
and
findWeatherByLocation
, how do I trigger the
callback
?

Thanks for your help in advance.

Answer

Well this question doesn't belong to StackOverflow but here is how you can do this:

function responseHandler (error, response, body, callback) {
    if (!error && response.statusCode == 200) {
        let weather = getWeatherReport(JSON.parse(body));
        callback(weather ? weather : null);
    }
    else {
        console.error(response.error);
        callback(null);
    }
}

function findWeatherForCity(senderID, city, countryCode, callback) {
    //Lets configure and request
    request({
        url: constants.OPEN_WEATHER_MAP_BASE_URL, //URL to hit
        qs: {
            q: city + ',' + countryCode,
            appid: constants.OPEN_WEATHER_MAP_API_KEY
        }, //Query string data
        method: 'GET', //Specify the method
    }, function(error, response, body) {
        responseHandler(error, response, body, callback)
    });
}

/*
 lat, lon coordinates of the location of your interest
 * http://openweathermap.org/current
 */

function findWeatherForLocation(senderID, location, callback) {
    //Lets configure and request
    request({
        url: constants.OPEN_WEATHER_MAP_BASE_URL, //URL to hit
        qs: {
            lat: location.lat,
            lon: location.lon,
            appid: constants.OPEN_WEATHER_MAP_API_KEY
        }, //Query string data
        method: 'GET', //Specify the method
    }, function(error, response, body) {
        responseHandler(error, response, body, callback)
    });
}