0

如何将其重构为 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
    }, ...
4

1 回答 1

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();
            });
        }
    });
};
于 2013-11-01T13:52:06.353 回答