Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 中国的意外承诺_Javascript_Reactjs_Api_Es6 Promise_Fetch Api - Fatal编程技术网

Javascript 中国的意外承诺

Javascript 中国的意外承诺,javascript,reactjs,api,es6-promise,fetch-api,Javascript,Reactjs,Api,Es6 Promise,Fetch Api,一般来说,我对本地和编码都不熟悉。我为upwork上的一些代码付费,我很难将其集成到我的程序中 async pullBatch(since){ let param = { userScreenName: '?screen_name=google', count: "&count=5", retweets: "&include_rts=false", replies: "&exclude_replies

一般来说,我对本地和编码都不熟悉。我为upwork上的一些代码付费,我很难将其集成到我的程序中

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)
}
async pullBatch(自){
设param={
userScreenName:“?screen_name=google”,
计数:“&count=5”,
转发:“&include\u rts=false”,
答复:“&exclude_replies=false”,
修剪:“&trim\u user=true”,
自:“&max_id=“+自
};
让twitterRest=new twitterRest();//创建twitterRest类的新实例
let batch=wait twitterRest.pullTweets(param);//拉谷歌时间线
退货批次;
}
pullTimeline(){
让时间线=[]

对于(i=0;i让我们将其分解。您似乎理解
pullBatch
是一个异步函数,因此调用它将返回twitterRest交互创建的承诺

问题是,在for循环中调用
pullBatch
无法解决这些承诺(这似乎是您想要做的)。最简单的方法是对
currentBatch
使用
wait
,但在尝试时,您得到了保留错误。基本上,您只需将
pullTimeline
设置为异步,如下所示:

async pullTimeline(){
  ...
只要意识到,一旦您这样做,
pullTimeline
现在是一个异步函数,它也将返回一个承诺。因此,要使用此函数,您需要使用
。然后()
,例如:

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

// call the function above, and just disregard its promise
useTimeLine()
或者,如果您在另一个异步函数中使用它,则可以使用wait

const timeLine = await pullTimeline() // must be inside async function
基本上,在调用链的某个点上,您必须使用
.then()
解析承诺,或者通过创建顶级异步函数来忽略顶级承诺。例如:

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

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

只是别忘了在某个地方处理错误。在你的顶级承诺中使用
.catch()
,或者在你的任何等待电话中使用
try/catch

这是一个深思熟虑和有用的回复。非常感谢。接受了你的建议,经过一点修改(比我自豪地承认的更多的猜测和检查)我把事情办好了,再次谢谢