简体   繁体   English

React-Native的睡眠功能?

[英]Sleep function for React-Native?

So I'm trying to fetch all 'places' given some location in React Native via the Google Places API. 因此,我试图通过Google Places API获取React Native中的某些位置来获取所有“位置”。 The problem is that after making the first call to the API, Google only returns 20 entries, and then returns a next_page_token , to be appended to the same API call url . 问题是,在第一次调用API之后,Google只返回20个条目,然后返回next_page_token ,以附加到同一个API调用url So, I make another request to get the next 20 locations right after, but there is a small delay (1-3 seconds) until the token actually becomes valid, so my request errors. 因此,我提出另一个请求,以便立即获得接下来的20个位置,但是在令牌实际变为有效之前会有一个小的延迟(1-3秒),因此我的请求出错。

I tried doing: 我试过做:

this.setTimeout(() => {this.setState({timePassed: true})}, 3000);

But it's completely ignored by the app...any suggestions? 但它被应用程序完全忽略了......有什么建议吗?

Update 更新

I do this in my componentWillMount function (after defining the variables of course), and call the setTimeout right after this line. 我在componentWillMount函数中执行此操作(在定义变量之后),并在此行之后调用setTimeout

axios.get(baseUrl)
.then((response) => {
   this.setState({places: response.data.results, nextPageToken: response.data.next_page_token });

});

What I understood is that you are trying to make a fetch based on the result of another fetch. 我的理解是你试图根据另一个提取的结果进行提取。 So, your solution is to use a TimeOut to guess when the request will finish and then do another request, right ? 所以,你的解决方案是使用TimeOut来猜测请求何时完成,然后再做另一个请求,对吗?

If yes, maybe this isn't the best solution to your problem. 如果是,也许这不是解决问题的最佳方案。 But the following code is how I do to use timeouts: 但是以下代码是我如何使用超时:

// Without "this"
setTimeout(someMethod,
    2000
)

The approach I would take is to wait until the fetch finishes, then I would use the callback to the same fetch again with different parameters, in your case, the nextPageToken . 我将采取的方法是等待提取完成,然后我将使用不同的参数再次使用回调来进行相同的提取,在您的情况下,使用nextPageToken I do this using the ES7 async & await syntax. 我使用ES7 async和await语法执行此操作。

// Remember to add some stop condition on this recursive method.
async fetchData(nextPageToken){
    try {
        var result = await fetch(URL)
        // Do whatever you want with this result, including getting the next token or updating the UI (via setting the State)
        fetchData(result.nextPageToken)
    } catch(e){
         // Show an error message
    }
}

If I misunderstood something or you have any questions, feel free to ask! 如果我误解了某些内容或者您有任何疑问,请随时提出!

I hope it helps. 我希望它有所帮助。

try this it worked for me: 试试这个对我有用:

  async componentDidMount() {
     const data = await this.performTimeConsumingTask();

        if (data !== null) {
       // alert('Moved to next Screen here');
    this.props.navigator.push({
        screen:"Project1.AuthScreen"})
        }
  }
  performTimeConsumingTask = async() => {
    return new Promise((resolve) =>
      setTimeout(
        () => { resolve('result') },
        3000
      )
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM