Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.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 什么';(使用Typescript&;AngularJS)编写轮询方法的最佳(正确)方法是什么?_Javascript_Ajax_Angularjs_Typescript - Fatal编程技术网

Javascript 什么';(使用Typescript&;AngularJS)编写轮询方法的最佳(正确)方法是什么?

Javascript 什么';(使用Typescript&;AngularJS)编写轮询方法的最佳(正确)方法是什么?,javascript,ajax,angularjs,typescript,Javascript,Ajax,Angularjs,Typescript,我正在尝试编写一个轮询方法,定期轮询服务器,以检查是否已经创建了zip文件 我想完成以下几点: 调用(ajax)在服务器上创建zip文件的API 调用(ajax)另一个API,检查zip文件是否已创建(轮询方法) 一些后续过程 这是我的代码片段↓ var success: boolean = false; //1. requests a server to create a zip file this.apiRequest.downloadRequest(params,ApiUrl.URL_FO

我正在尝试编写一个轮询方法,定期轮询服务器,以检查是否已经创建了zip文件

我想完成以下几点:

  • 调用(ajax)在服务器上创建zip文件的API
  • 调用(ajax)另一个API,检查zip文件是否已创建(轮询方法)
  • 一些后续过程
  • 这是我的代码片段↓

    var success: boolean = false;
    //1. requests a server to create a zip file
    this.apiRequest.downloadRequest(params,ApiUrl.URL_FOR_DOWNLOAD_REQUEST)
    .then((resObj) => {
           var apiRes: IDownloadService = resObj.data;
           if (apiRes.status[0].statusCode == "000") {
                success = true;
           } else {
                //Error
           }
    }).then(() => {
           if (success) {
             //2. polls the server to check if the zip file is ready
             <- Polling method↓ ->
             this.polling(params).then((zipUrl) => {
                        console.log(zipUrl); //always logs zipUrl
                        //some subsequent process...
             });
           }
    });
    
    var成功:boolean=false;
    //1. 请求服务器创建zip文件
    this.apiRequest.downloadRequest(参数,apirl.URL用于下载请求)
    .然后((resObj)=>{
    var apiRes:IDownloadService=resObj.data;
    如果(apiRes.status[0].statusCode==“000”){
    成功=真实;
    }否则{
    //错误
    }
    }).然后(()=>{
    如果(成功){
    //2.轮询服务器以检查zip文件是否准备就绪
    this.polling(params).then((zipUrl)=>{
    console.log(zipUrl);//始终记录zipUrl
    //一些后续过程。。。
    });
    }
    });
    
    有人能举一些在这种情况下有效的轮询方法的例子吗

    增加:

    private polling(params: any): ng.IPromise<any> {
                var poller = () => this.apiRequest.polling(params, ApiUrl.URL_FOR_POLLING);
                var continuation = () => poller().then((resObj) => {
                    var apiRes: IDownloadService = resObj.data;
                    if (apiRes.zipFilePath == "") {
                        return this.$timeout(continuation, 1000);
                    } else {
                        return apiRes.zipFilePath;
                    }
                })
                var result: ng.IPromise<any> = continuation();
                return result;          
            }
    
    私有轮询(参数:任意):ng.IPromise{
    var poller=()=>this.apiRequest.polling(params,apirl.URL_用于_轮询);
    var continuation=()=>poller()。然后((resObj)=>{
    var apiRes:IDownloadService=resObj.data;
    如果(apiRes.zipFilePath==“”){
    返回此值。$timeout(续,1000);
    }否则{
    返回apiRes.zipFilePath;
    }
    })
    var结果:ng.IPromise=continuation();
    返回结果;
    }
    
    基本上将方法抽象出来,如下所示:

    let poll = () => this.apiRequest.downloadRequest(params,ApiUrl.URL_FOR_DOWNLOAD_REQUEST)
    
    let continuation = () => poll().then((/*something*/)=> {
     /*if still bad*/ return continuation();
     /*else */ return good;
    })
    
    continuation().then((/*definitely good*/));
    
    更新 根据以下评论中的要求:

    返回此值。$timeout(续,1000)


    这是启动摘要循环所必需的。

    添加的是我实际的轮询方法,它每1秒轮询一次服务器,以检查请求的zip文件是否已经创建并且工作正常。我在这里的问题是,为什么在前面有和没有“return”的情况下,方法的行为会不同。$timeout(continuation,1000);你能帮我理解这一点吗?实际上我想知道的是,当在“this.$timeout(continuation,1000);”@basarat之前加上和不加“return”时,轮询方法的行为如何?@basarat我喜欢这样,但最后一行的目的是什么:continuation()。然后(/*绝对好*/);