【问题标题】:Run request until response is what was requested with $q运行请求,直到响应是 $q 请求的内容
【发布时间】:2015-11-05 09:50:27
【问题描述】:

我有这个 plunker 来说明我的问题:

http://plnkr.co/edit/tsBy2K0xv6bboBlZRM8c

$scope.start = function() {

  runThisUntil("Ok");
  //here I would like to do this:
  //runThisUntil("Ok").then(function () {
  //doSomeStuff()  
  //});

}

$scope.clear = function() {
  $scope.responses = [];
}

function runThisUntil(criteria) {

  runThis(criteria);



}

function runThis(criteria) {
  run().then(function (response) {
    if (response == criteria) {
      $scope.responses.push("Done");
    } else {
      $scope.responses.push("Wait");
      runThisUntil(criteria);
    }
  });
}


var okWhen = 10;
var start = 0;
function run() {

  var deferred = $q.defer();
  $timeout(function () {
    if (start !== okWhen) {
    start += 1;
    deferred.resolve("Bad");
  } else {
    deferred.resolve("Ok")
    start = 0;
  }
  }, 100);


  return deferred.promise;


}
}

我在这里试图模拟的是一种循环形式,在这种循环中,我向一个可以批量工作的 http 服务器发出请求,并以“还有更多工作”作为响应,直到“工作完成”。

当我尝试使用 Promise 执行此操作时,我最终使用 $q 创建了新的延迟 Promise,因此我等待解决的初始 Promise 从未得到解决,因为我通过执行与我相同的函数来重复请求如果没有完成,我会再次加入。

--编辑 我想我可以在完成后使用 $scope.$broadcast() 来解决这个问题,但如果可能的话,我想通过使用 $q 来解决这个问题,如果可能的话不要监听事件。

【问题讨论】:

    标签: javascript angularjs promise q deferred


    【解决方案1】:

    更新:

    var app = angular.module('myApp', []);
    
    angular.module('myApp').controller('TestCtrl', ["$timeout", "$interval", "$scope", "$q", function TestCtrl($timeout, $interval, $scope, $q) {
    
      activate();
    
      $scope.responses = [];
    
      function activate() {
    
        $scope.start = function() {
    
          runThisUntil("Ok").then(function() {
            $scope.responses.push("Pushed final");;
          });
        }
    
        $scope.clear = function() {
          $scope.responses = [];
        }
    
        function runThisUntil(criteria) {
    
          return runThis(criteria);
        }
    
        function runThis(criteria) {
          return run().then(function (response) {
            $scope.responses.push("Done");
          }, function () {
            $scope.responses.push("Wait");
            return runThis(criteria);
          });
        }
    
    
        var okWhen = 10;
        var start = 0;
        function run() {
    
          var deferred = $q.defer();
          $timeout(function () {
            if (start !== okWhen) {
            start += 1;
            deferred.reject("Bad");
          } else {
            deferred.resolve("Ok")
            start = 0;
          }
          }, 100);
    
          return deferred.promise;
        }
    
      }
    
    
    }]);
    

    http://plnkr.co/edit/MYn6otWMmxHFDd41jBpI?p=preview

    【讨论】:

    • 你能用一个工作的 plunker 来说明一下吗?这不能作为 runThis 不返回承诺
    • 太棒了!谢谢,需要理解这一点,但是这正是我想要的:)
    • 你需要稍微研究一下promise。 resolve() 和 reject() 可以做一些很棒的事情。
    • 如何将数据传递给最终事件?我想这样做:runThisUntil("Ok").then(function(response) { $scope.responses.push(response); });但响应未定义
    • 需要在then函数中返回。
    猜你喜欢
    • 1970-01-01
    • 2016-11-22
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多