I have two API-calls chained like so:
let weatherPromise;
if (crds) {
weatherPromise = mapbox.getLocation(crds)
.then(locData => darksky.getWeather(crds))
.then(weatherData => Object.assign(weatherData, {city: locData.city}));
} else if (term) {
weatherPromise = mapbox.getCoords(term)
.then(locData => darksky.getWeather(locData.crds));
} else {
weatherPromise = Promise.reject({error: 'Aborted due to unexpected POST-Body'});
}
weatherPromise.then(weatherData => {
io.emit('update', weatherData);
console.log(`Answer sent: ${JSON.stringify(weatherData)}`);
}, reject => {
io.emit('update', reject);
console.error(reject.error);
});
But it is not working at all. The error-handling is all over the place (mainly just logging undefined
and failing) and for example locData
doesn't seem to be accessible in that second then()
anymore. I tried nesting Promises and it worked fine but I would like to avoid that anti-pattern if I can.