AngularJS Promise 重试 [英] AngularJS Promise retries

查看:24
本文介绍了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.

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

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) 返回该启动查询的承诺.像这样:

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

以下是您将如何开始:

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

这种方法的一个好处是,即使您传递给它的函数在调用它时抛出错误,或者根本不返回承诺,重试功能和通过已解决的承诺返回结果仍然会工作.

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天全站免登陆