【发布时间】:2019-07-23 18:55:01
【问题描述】:
我正在尝试使用 react-geocode 插件,并将它放在 redux saga 的生成器函数中。我对 Promise 和 redux-saga 还是很陌生,所以我不确定这是处理这个插件或一般 Promise 的最佳方式。我正在考虑使用 $scope 在承诺之外调用 lat 和 lng 变量,但我不确定这是否是解决这个问题的最佳方法。最后,lat 和 lng 返回 undefined 但不会捕获错误。
function* getSearch() {
const address = yield select(state => state.location.address);
Geocode.setApiKey("****************");
console.log(address)
const a = Geocode.fromAddress(address).then(
response => {
const { lat, lng } = response.results[0].geometry.location;
},
error => {
console.error(error);
}
)
try {
yield put({ type: 'SUCCESS_FETCH_LOCATION', payload: {lat: a.lat, lng: a.lng}})
} catch (error) {
console.log(error);
}
}
【问题讨论】:
标签: reactjs promise redux-saga google-geocoder