如何避免附加代码段中JavaScript中的代码重复?

时间:2016-08-13 07:46:40

标签: javascript callback overloading

我正在尝试使用Open Weather地图查找天气,我有两种方法,findWeatherByLocationfindWeatherByCity。我假设JavaScript不支持method overloading,因此有2个不同的名称。两种方法都接受将被触发的callback函数并执行相同的操作。

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);
        }
    });
}

正如您所看到的,function(error, response, body)在两个地方都做同样的事情。如果我为function(error, response, body)findWeatherByCity分别设置findWeatherByLocation,我该如何触发callback

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这个问题不属于StackOverflow,但是你可以这样做:

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)
    });
}