2016-08-13 82 views
0

我想使用Open Weather地圖找到天氣,我有2種方法,findWeatherByLocationfindWeatherByCity。我假設JavaScript不支持method overloading,因此不支持2個不同的名稱。兩種方法都接受將被觸發的callback函數,並執行相同的操作。如何避免附加片段中的JavaScript代碼重複?

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

感謝您的幫助提前。

+1

我投票作爲題外話,因爲它屬於http://codereview.stackexchange.com/ –

回答

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

非常尊重,這並不好看,關閉這個問題,處理的ErrorHandler成功藏漢?顯然這個代碼可以被重構,比這更重要... – Bamieh

+0

我完全不理解這個@AhmadBamieh,你的代碼也處理成功以及失敗。 –

1

我用承諾來重構回調整潔的代碼,但你可以回調雖然替換它們我不推薦它(已經是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); 
 
    });