2016-08-13 6 views
0

Ich versuche, das Wetter mit Open Weather Karte zu finden, und ich habe 2 Methoden, findWeatherByLocation und findWeatherByCity. Ich gehe davon aus, dass JavaScriptmethod overloading und damit die 2 verschiedenen Namen nicht unterstützt. Beide Methoden akzeptieren eine callback Funktion, die ausgelöst wird und das Gleiche tut.Wie vermeidet man Code-Duplizierung in JavaScript im angehängten Snippet?

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

Wie Sie sehen können, hat die function(error, response, body) das gleiche in beiden Orten. Wenn ich eine separate function(error, response, body) mache, die sowohl für findWeatherByCity und findWeatherByLocation ist, wie trigger ich die callback?

Vielen Dank für Ihre Hilfe im Voraus.

+1

Ich wähle diese Frage zu schließen, wie Off-Topic, weil es –

Antwort

0

Nun, diese Frage zu Stackoverflow nicht gehört, aber hier ist, wie Sie dies tun können:

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

mit großem Respekt dieses nicht nett http://codereview.stackexchange.com/ gehört aussehen, Griffe errorhandler Erfolg auch?? offensichtlich kann der Code viel mehr als dies auch refaktoriert werden ... – Bamieh

+0

Ich verstehe das nicht wirklich @AhmadBamieh, Ihr gegebener Code ist auch der Erfolg und Misserfolg. –

1

I Versprechen verwendet haben die Rückrufe und ordentlich den Code Refactoring, aber Sie können sie mit Rückrufe ersetzen, obwohl Ich empfehle es nicht (es ist bereits 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); 
 
    });

Verwandte Themen