如何将此重构为Promise / A模式,以便我可以消除定时器并只处理发回的Promise?
$scope.manageAccess = function () {
queuedRepository.queuedItems().then(function (queuedItems) {
if (queuedItems.length === 0) {
var path = globalLocation.pathname(),
hash = globalLocation.hashNoBang();
globalLocation.url(app.vroot() + "SharedSubmission/" + $scope.data.submissionVersion.id + "?path=" + path + "&hash=" + hash);
} else {
console.log("Pending items in the queue... Retrying in 500ms.");
setTimeout(function () {
$scope.manageAccess();
}, 500);
}
});
};
排队存储库
return {
queuedItems: function() {
return persistentCache.list('qr'); // Return Promise
}, ...
答案 0 :(得分:1)
function delay(ms) {
var d = $q.defer();
setTimeout(function(){
d.resolve();
}, ms);
return d.promise;
}
$scope.manageAccess = function () {
return queuedRepository.queuedItems().then(function (queuedItems) {
if (queuedItems.length === 0) {
var path = globalLocation.pathname(),
hash = globalLocation.hashNoBang();
return globalLocation.url(app.vroot() + "SharedSubmission/" + $scope.data.submissionVersion.id + "?path=" + path + "&hash=" + hash);
} else {
return delay(500).then(function(){
return $scope.manageAccess();
});
}
});
};