简体   繁体   English

React Native中的意外承诺

[英]Unexpected Promise in React Native

I am new to React Native and coding in general. 我是React Native和编码的新手。 I paid for some code on upwork and am having a hard time integrating it in my program. 我花了一些代码来完成工作,并且很难将其集成到我的程序中。

async pullBatch(since){
    let param = {
        userScreenName: '?screen_name=google',
        count: "&count=5",
        retweets: "&include_rts=false",
        replies: "&exclude_replies=false",
        trim: "&trim_user=true",
        since: "&max_id=" + since
    };

    let twitterRest = new TwitterRest(); //create a new instance of TwitterRest Class   
    let batch = await twitterRest.pullTweets(param); //pull the Google TimeLine
    return batch;
}

pullTimeline(){
    let timeLine = []
    for(i = 0; i <= 2; i++){
        let currentBatch = this.pullBatch("1098740934588751900")
        console.log(currentBatch);
        timeLine = timeLine.concat(currentBatch);
    }
    console.log(timeLine);
    // timeLine = currentBatch
    return(timeLine)
}

I believe that when running pullTimeLine() the program is returning an array of three promises. 我相信,当运行pullTimeLine()时,程序将返回三个promise的数组。 (I have also run the code with "await" before pullBatch(), but it is erroring out telling me await is a reserved word) This means I am making two mistakes: (在pullBatch()之前,我也使用“ await”运行了代码,但是错误地告诉我await是保留字)这意味着我犯了两个错误:

  1. I am not correctly understanding promises in JS or how they are resolved. 我没有正确理解JS中的承诺或如何解决它们。
  2. I am incorrectly concatenating the arrays. 我错误地串联了数组。

I am constantly trying to learn, so while I greatly appreciate suggestions for code fixes, I also really would appreciate if you'd teach me about where my lapses in understanding lies. 我一直在努力学习,因此,尽管我非常感谢有关代码修复的建议,但如果您能教给我有关理解的错误之处,我也将不胜感激。

Thank you 谢谢

Let's break it down. 让我们分解一下。 You seem to understand that pullBatch is an async function, and so calling it will return a promise create by the twitterRest interaction. 您似乎了解了pullBatch是一个异步函数,因此调用它会返回由twitterRest交互创建的Promise。

The problem is that your call to pullBatch inside your for loop will not resolve these promise (which seems to be what you want to do). 问题是您在for循环中对pullBatch的调用将无法解决这些承诺(这似乎是您想要做的)。 The easiest way is to use await for currentBatch , but as you tried, you got the reserved error. 最简单的方法是对currentBatch使用await ,但是在尝试时得到了保留的错误。 Basically you just need to also make pullTimeline async like this: 基本上,您只需要使pullTimeline异步,如下所示:

async pullTimeline(){
  ...

Just realise that once you do this, pullTimeline is now an async function that will also return a promise. 只要意识到,一旦执行此操作, pullTimeline现在就是一个异步函数,该函数还将返回一个pullTimeline So to use this function you need to either use .then() , for example: 因此,要使用此功能,您需要使用.then() ,例如:

pullTimeline().then(timeLine => {
  // do something with your timeline here
})

Or if you are using it within another async function, you can use await. 或者,如果您在另一个异步函数中使用它,则可以使用await。

const timeLine = await pullTimeline() // must be inside async function

Basically at some point in your calling chain, you will have to resolve a promise using .then() , or disregard the top level promise by making a top level async function. 基本上,在调用链中的某个时刻,您将必须使用.then()来解决promise,或者通过创建顶级异步函数来忽略顶级promise。 For example: 例如:

async useTimeline() {
  const timeLine = await pullTimeline()
  // do something with your timeline
}

// call the function above, and just disregard its promise
useTimeLine()

Just don't forget to handle errors somewhere. 只是不要忘记在某个地方处理错误。 Either use a .catch() on your top level promise, or use try / catch around any of your await calls. 在顶级.catch()上使用.catch() ,或在任何等待调用中使用try / catch

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

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