AngularJS Promise重试 [英] AngularJS Promise retries

查看:86
本文介绍了AngularJS Promise重试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使重试功能正常工作时遇到了一些麻烦,并希望获得一些帮助.我有一个要调用的$ resource,直到出现成功情况或超过最大重试次数为止.

I am having some trouble getting a retry function to work and was hoping for some assistance. I have a $resource that I want to have called until a success condition occurs or the maximum retries has been exceeded.

我似乎遇到的问题是,在我的重试功能中,我正在调用另一个promise,而这就是检查条件的地方.通过几次重试后,删除添加的promise并创建默认的成功条件,我可以使代码正常运行,但是我无法弄清楚如何正确地将新的promise调用添加到函数中.

The issue I seem to be running into is that within my retry function I am calling another promise and that is where the condition would be checked. I could get the code to function as intended by removing the added promise and creating a default success condition after a few retries but I cannot figure out how to correctly add the new promise call into the function.

resource是Angular $ resource的替代品,它返回$ promise

resource is a stand-in for an Angular $resource which returns a $promise

我的代码如下:

resource.$promise.then(function (response) {
  return keepTrying(state, 5);
 }).then(function (response) {

 }).catch(function (err) {
   console.log(err);
 });

以及keepTrying函数:

And the keepTrying function:

function keepTrying(state, maxRetries, deferred) {
  deferred = deferred || $q.defer();
  resource.$promise.then(function (response) {
    success = response;
  });
  if (success) {
    return deferred.resolve(success);
  } else if (maxRetries > 0) {
    setTimeout(function () {
      keepTrying(state, maxRetries - 1, deferred);
     }, 1000);
   } else if (maxRetries === 0) {
     deferred.reject('Maximum retries exceeded');
   }
   return deferred.promise;
 }

推荐答案

尝试的问题在于,您不是在重新查询资源,而是一遍又一遍地使用对已查询资源的承诺.

The problem with your attempt is that you are not re-querying the resource, but using the promise for an already-queried resource over and over.

您需要做的是使用一个函数,该函数将(a)启动查询,并(b)返回该启动查询的promise.像这样:

What you need to do is use a function that will (a) initiate the query, and (b) return the promise for that initiated query. Something like this:

function () { return $resource.get().$promise; }

然后,您可以将其传递给类似的内容,然后进行重试.

Then you can pass it into something like this, that will do the retries.

function retryAction(action, numTries) {
    return $q.when()
        .then(action)
        .catch(function (error) {
            if (numTries <= 0) {
                throw error;
            }
            return retryAction(action, numTries - 1);
        });
}

这是您要如何开始的方法:

Here's how you would start this off:

retryAction(function () { return $resource.get().$promise; }, 5)
.then(function (result) {
    // do something with result 
});

关于此方法的一件好事是,即使传递给它的函数在调用它时抛出错误,或者根本不返回promise,重试功能和通过已解决的promise返回结果仍然可以工作.

One nice thing about this approach is that even if the function that you pass to it throws an error upon invoking it, or doesn't return a promise at all, the retry functionality and the returning of the result via a resolved promise will still work.

示例

这篇关于AngularJS Promise重试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆