Javascript 重构为承诺/模式

Javascript 重构为承诺/模式,javascript,promise,Javascript,Promise,我如何将其重构为Promise/A模式,以便消除计时器并只处理发送回的Promise $scope.manageAccess = function () { queuedRepository.queuedItems().then(function (queuedItems) { if (queuedItems.length === 0) { var path = globalLocation.path

我如何将其重构为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
    }, ...
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();
            });
        }
    });
};