[英]How to avoid code duplication in JavaScript in the attached snippet?
我正在嘗試使用Open Weather
地圖查找天氣,並且我有2種方法,即findWeatherByLocation
和findWeatherByCity
。 我假設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
?
感謝您的幫助。
我已經使用了promises來重構回調並整理代碼,但是您可以用回調代替它們,盡管我不建議這樣做(已經是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); });
這個問題不屬於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)
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.