最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the best(right) way to write a polling method (with Typescript & AngularJS)? - Stack Overflo

programmeradmin0浏览0评论

I am trying to write a polling method that polls a server periodically to check whether a zip file has already been created or not.

What I want to acplish are the following:

  1. Calls(ajax) an API that creates a zip file on server
  2. Calls(ajax) another API that checks if the zip file has already been created (polling method)
  3. Some subsequent process

Here is my code snippet ↓

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...
         });
       }
});

Could anyone give some examples of polling method that would work in this case?

Added:

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;          
        }

I am trying to write a polling method that polls a server periodically to check whether a zip file has already been created or not.

What I want to acplish are the following:

  1. Calls(ajax) an API that creates a zip file on server
  2. Calls(ajax) another API that checks if the zip file has already been created (polling method)
  3. Some subsequent process

Here is my code snippet ↓

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...
         });
       }
});

Could anyone give some examples of polling method that would work in this case?

Added:

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;          
        }
Share Improve this question edited Aug 21, 2015 at 5:17 d-_-b asked Aug 20, 2015 at 8:58 d-_-bd-_-b 4,5229 gold badges31 silver badges47 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Basically abstract the methods out as shown below:

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*/));

Update

As requested in the ment below:

return this.$timeout(continuation, 1000);

This is needed to get angular to kick off a digest cycle.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论