【问题标题】:How to avoid code duplication in JavaScript in the attached snippet?如何避免附加代码段中 JavaScript 中的代码重复?
【发布时间】:2016-08-13 07:46:40
【问题描述】:

我正在尝试使用Open Weather 地图查找天气,我有两种方法,findWeatherByLocationfindWeatherByCity。我假设JavaScript 不支持method overloading,因此有两个不同的名称。这两种方法都接受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),这对于findWeatherByCityfindWeatherByLocation 都是通用的,我该如何触发callback

提前感谢您的帮助。

【问题讨论】:

标签: javascript callback overloading


【解决方案1】:

我已经使用 promise 来重构回调并整理代码,但你可以用回调替换它们,尽管我不推荐它(已经是 2016 年了)。

/* you are not using senderID anywhere but i left it cuz you had it.*/
function findWeather(senderID, queryType, options) {
  return new Promise(function(resolve, reject) {
    var queryObj = {
      appid: constants.OPEN_WEATHER_MAP_API_KEY
    };
    if (queryType === 'city') {
      queryObj.q = options.city + ',' + options.countryCode;
    } else if (queryType === 'location') {
      queryObj.lat= options.lat;
      queryObj.lon= options.lon;
      }
    } else {
      reject('no valid queryType');
    }

    request({
      url: constants.OPEN_WEATHER_MAP_BASE_URL,
      qs: queryObj,
      method: 'GET'
    }, function(err, response, body) {
      if (!error && response.statusCode == 200) {
        let report = getWeatherReport(JSON.parse(body));
        resolve(report ? report : null);
      } else {
        reject(response.error);
      }
    });
  });
}

/*USAGE*/
findWeather(1, 'city', {
    city: 'Ramallah',
    countryCode: '00970'
  })
  .then(function(data) {
    console.log(data);
  });

findWeather(1, 'location', {
    lat: 1,
    lon: 2
  })
  .then(function(data) {
    console.log(data);
  })
  .catch(function(err) {
    console.log(err);
  });

【讨论】:

    【解决方案2】:

    这个问题不属于 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)
        });
    }
    

    【讨论】:

    • 非常尊重这看起来不太好,errorHandler 也处理成功??显然代码可以重构的远不止这些......
    • 我很不明白这个@AhmadBamieh,你给定的代码也在处理成功和失败。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    相关资源
    最近更新 更多