Reactjs 如何在生成器函数中使用react地理代码承诺

Reactjs 如何在生成器函数中使用react地理代码承诺,reactjs,promise,redux-saga,google-geocoder,Reactjs,Promise,Redux Saga,Google Geocoder,我正在尝试使用react geocode插件,并将其放入redux传奇中的生成器函数中。我对promise和redux传奇还很陌生,所以我不确定这是处理这个插件或promise的最好方法。我曾考虑使用$scope调用承诺之外的lat和lng变量,但我不确定这是否是解决此问题的最佳方法。最后,lat和lng返回未定义,但不捕获错误 function* getSearch() { const address = yield select(state => state.location

我正在尝试使用react geocode插件,并将其放入redux传奇中的生成器函数中。我对promise和redux传奇还很陌生,所以我不确定这是处理这个插件或promise的最好方法。我曾考虑使用$scope调用承诺之外的lat和lng变量,但我不确定这是否是解决此问题的最佳方法。最后,lat和lng返回未定义,但不捕获错误

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

如果您做出承诺,redux saga将等待该承诺得到解决,然后在得到结果后恢复代码:

function* getSearch() {
  const address = yield select(state => state.location.address);
  try {
    const response = yield Geocode.fromAddress(address);
    const { lat, lng } = response.results[0].geometry.location;
    yield put({ type: 'SUCCESS_FETCH_LOCATION', payload: { lat, lng }});
  } catch (error) {
    console.log(error);
  }
}

如果您做出承诺,redux saga将等待该承诺得到解决,然后在得到结果后恢复代码:

function* getSearch() {
  const address = yield select(state => state.location.address);
  try {
    const response = yield Geocode.fromAddress(address);
    const { lat, lng } = response.results[0].geometry.location;
    yield put({ type: 'SUCCESS_FETCH_LOCATION', payload: { lat, lng }});
  } catch (error) {
    console.log(error);
  }
}