React native 使用嵌套异步进行本机反应无法正常工作

React native 使用嵌套异步进行本机反应无法正常工作,react-native,promise,async-await,React Native,Promise,Async Await,我有一些互相受益的方法,我希望其中一种方法能一个接一个地开始,但它不能完全按照我想要的方式工作。在另一个问题中,我在一个普通的方法中使用了async和Wait,但我没有设法使用GoogleAPI和嵌套函数 async componentDidMount() { await this.getCityList(); console.log(this.state.cities) await this.currentLocation(); await this.getVe

我有一些互相受益的方法,我希望其中一种方法能一个接一个地开始,但它不能完全按照我想要的方式工作。在另一个问题中,我在一个普通的方法中使用了async和Wait,但我没有设法使用GoogleAPI和嵌套函数

async componentDidMount() {
    await this.getCityList();
    console.log(this.state.cities)
    await this.currentLocation();
    await this.getVenueInformation();
  }
此时,第一个函数正常工作,在传递了所需的函数后,它将转到第二个方法。同样地,我必须将第二种方法的输入放入第三种方法,但我失败了

Current location method:
    currentLocation() {
        Geocoder.init(...);
        Geolocation.getCurrentPosition((position) => {
          this.getCurrentLoc(position.coords.latitude, position.coords.longitude),
            this.setState({
              region: {
                latitude: position.coords.latitude,
                longitude: position.coords.longitude,
              },
              error: null,
            });
        }, (error) => this.setState({ error: error.message }), { enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 });
      }
我想用上面的方法找到我的位置,然后用getcurrentloc方法得到地址信息,我计划根据这些信息显示附近的一些地方。然而,我不能让这个系统按顺序工作,我的地址总是空的

  getCurrentLoc(lat, longt) {
    Geocoder.from(lat, longt)
      .then(json => {....
        this.setState({...},
          this.getLocIDS(cityName, districtName, neighborhoodName)
        );
      })
      .catch(error => console.warn(error));
  }
此时,我通过address从数据库中获取地址,以提取id信息

  getLocIDS(cityName, districtName, neighborhoodName) {
    let link = ..;
    fetch(link)
      .then(response => response.json())
      .then(res => {
        this.setState({,,,,,,})
      })
      .catch(error => console.warn(error));
  }
最后,我想捕获场馆信息,但此时数据在数据库中输入为-1-1是我的初始状态值。我确信所有方法都能正常工作,但由于进程不是按顺序运行的,所以我的空间搜索方法在更新前一个函数状态中的数据之前工作

  getVenueInformation() {
    let link = ...
    fetch(link)
      .then(response => response.json())
      .then(venues => {
          this.setState({
            markers: venues,
            isLoading: false,
          })
      })
  };

数据库输入返回:
>>>>从mekanlar中选择*其中mekanlar.mahalle_no=“-1”

您不显示如何将信息从一种方法传递到另一种方法。 如果您使用
this.state
,这就是您的问题所在

此.setState
不会同步更新状态。因此,在下次函数调用中,
this.state
中的值可能不正确

为了确保数据流是正确的,显式返回值更有意义,即使您在内部执行
setState

async componentDidMount() {
    const cityList = await this.getCityList();
    console.log(cityList)
    const location = await this.currentLocation(cityList);
    const venueInformation = await this.getVenueInformation(location);
}

我在async componentDidMount()中使用wait this.currentLocation();我将每个函数作为参数赋予在this.getCurrentLoc(position.coords.latitude,position.coords.longitude)和this.getLocaids(cityName,districtName,neighborhoodName)中启动的每个函数。但是,我在使用的getLocaids(cityName,districtName,neighborhoodName)方法中更新为setStatete,等待此.getVenueInformation()方法,但它的未使用更新状态,我从async componentDidMount()方法调用了它。如果您在异步性的使用上保持一致,那么您会发现理解代码和调试更容易。尝试重构一切以使用async/await,您会发现跟踪控制流更容易。您可以使用使setState可等待。