写一个轮询方法的最佳(正确)方式是什么?AngularJS)
What's the best(right) way to write a polling method (with Typescript & AngularJS)?
我正在尝试编写一个轮询方法,定期轮询服务器以检查是否已经创建了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...
});
}
});
谁能给出一些在这种情况下工作的轮询方法的例子?
补充道:
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;
}
将这些方法抽象出来,如下所示:
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*/));
<标题> 更新按照下面评论的要求:
返回。美元超时(延续,1000);
这是需要的角开始消化循环
标题>相关文章:
- React重新渲染但未显示正确的组件
- 我的jQuery插件参数没有正确启动,遇到了问题
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- Amazon S3 REST API大小不正确
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- Javascript Reg Exp不正确匹配
- Javascript计数器变量未显示正确的值
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 在localhost Dev Box上测试JSONP请求的最佳方式
- 有条件更新d3.js力图中节点的最佳方法
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 将日期时间作为 JSON 发送将无法在我的视图中正确显示
- DIV并排,位置不正确
- 从查询字符串参数推断出正确的数据类型
- 访问 json 对象的最佳或正确方法
- 加载正确的PhoneGap文件的最佳方法是什么
- AngularJS.有关服务的正确双向数据绑定的最佳实践
- 从单选按钮组中获取所有选定值的最佳(和正确)方法是什么?
- 写一个轮询方法的最佳(正确)方式是什么?AngularJS)
- 使用jQuery/Ajax/JS加载PHP文件和显示状态的正确或最佳过程