使用Angular中的$ q实现,如何才能使promise循环直到成功? [英] Using the $q implementation in Angular how can I loop a promise until success?
本文介绍了使用Angular中的$ q实现,如何才能使promise循环直到成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据我最近的其他问题,我正在尝试将数据持久保存到针对移动设备(不稳定的连接)的角度应用程序的服务器,因此它应该一直尝试请求,直到成功为止.
As per my other recent questions, I'm trying to persist data to a server for an angular application that's targeted at mobile devices (unstable connections), so it should keep trying the request until success.
我该如何兑现承诺?
此刻我有:
服务:
this.addObject = function addObject(object) {
var deferred = $q.defer();
var httpConfig = {
method: 'POST',
url: 'http://api.foo.com/bar',
data: object
}
setTimeout(function() {
$http(httpConfig).
success(function(data, status) {
deferred.resolve('Woohoo!');
}).
error(function(data, status) {
deferred.reject('Couldnt reach server this time...');
});
}, 3000);
return deferred.promise;
}
控制器:
myService.addObject(myObject)
.then(function(message) {
console.log(message);
}, function(message) {
console.log(message);
});
我无法删除拒绝回调,因为没有它,代码似乎无法执行,但是一旦调用拒绝,它就会中断setTimeout循环.我该如何强迫诺言重复直到成功回调?
I can't remove the reject callback, as the code won't seem to execute without it, but once the reject is called, it breaks the setTimeout loop. How can I force the promise to repeat until the success callback?
推荐答案
这是 查看全文