运行 请求直到响应是用 $q 请求的
Run request until response is what was requested with $q
我有这个 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 服务器发出请求,并使用 "There is still more work" 进行响应,直到 "Work is done"。
当我尝试使用 promise 执行此操作时,我最终使用 $q 创建了新的延迟 promise,因此我等待解决的初始 promise 从未得到解决,因为我通过执行相同的函数重复了请求如果没有完成,我会再次加入。
--编辑
我想我可以在完成后使用 $scope.$broadcast() 来做到这一点,但我想尽可能使用 $q 来解决这个问题,并且尽可能不监听事件。
更新:
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;
}
}
}]);
我有这个 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 服务器发出请求,并使用 "There is still more work" 进行响应,直到 "Work is done"。
当我尝试使用 promise 执行此操作时,我最终使用 $q 创建了新的延迟 promise,因此我等待解决的初始 promise 从未得到解决,因为我通过执行相同的函数重复了请求如果没有完成,我会再次加入。
--编辑 我想我可以在完成后使用 $scope.$broadcast() 来做到这一点,但我想尽可能使用 $q 来解决这个问题,并且尽可能不监听事件。
更新:
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;
}
}
}]);