在角链式承诺(Chained promises in Angular)

2019-09-28 00:52发布

我有我希望按顺序处理记录列表。 A“根”承诺需要被传递回被添加作为另一个父链的一部分。

processCategories = function(categories) {
    var deferred = $q.defer();
    var promise = deferred.promise;

    // Process each category in sequence
    angular.forEach(categories, function (data, classIndex) {
        promise = promise.then(doSomethingAndReturnAPromise(categories, data, classIndex));

        if (classIndex === categories.length - 1) {
            // Last category has been reached, resolve this unit of work
            promise.then(function() {
                deferred.resolve(categories);
            });
        }
    });

    // Pass the root promise back to the chain
    return promise;
}

doSomethingAndReturnAPromise = function(categories, data, index) {
    console.log('item : ' + index + ' = started');
    var deferred = $q.defer();

    executeAnAsynchronousCall(function () {
            console.log('item : ' + index + ' = done');
            deferred.resolve(categories);

    }, function(status, text) {
            console.log('item : ' + index + ' = error');
            // Handle error here

    }, data);

    console.log('item : ' + index + ' = returned');
    return deferred.promise;
}

我从我的异步调用得到了一些错误,因为执行是在同一时间发生的(而不是按顺序)。 我添加记录上述和观察到以下:

2014-08-08 11:38:02.702 item : 1 = started
2014-08-08 11:38:02.702 item : 1 = returned
2014-08-08 11:38:02.702 item : 2 = started
2014-08-08 11:38:02.703 item : 2 = returned
2014-08-08 11:38:02.703 item : 3 = started
2014-08-08 11:38:02.704 item : 3 = returned
2014-08-08 11:38:02.704 item : 4 = started
2014-08-08 11:38:02.705 item : 4 = returned
2014-08-08 11:38:02.705 item : 5 = started
2014-08-08 11:38:02.706 item : 5 = returned
2014-08-08 11:38:02.707 item : 6 = started
2014-08-08 11:38:02.707 item : 6 = returned
2014-08-08 11:38:02.708 item : 7 = started
2014-08-08 11:38:02.709 item : 7 = returned
2014-08-08 11:38:02.709 item : 8 = started
2014-08-08 11:38:02.710 item : 8 = returned

2014-08-08 11:38:02.716 item : 2 = error
2014-08-08 11:38:02.718 item : 3 = error
2014-08-08 11:38:02.719 item : 4 = error
2014-08-08 11:38:02.720 item : 5 = error
2014-08-08 11:38:02.722 item : 1 = done // (This has finished, the server is now expecting #2)
2014-08-08 11:38:02.723 item : 6 = error
2014-08-08 11:38:02.735 item : 8 = error
2014-08-08 11:38:02.754 item : 7 = error 

我期待的.then ,以确保承诺按顺序运行,但每次我运行的代码时似乎他们都被一次发射。

任何人都可以看到我要去哪里错了吗?

Answer 1:

你的第二个行调用该函数,而不是调度它被调用后

更改:

promise = promise.then(doSomethingAndReturnAPromise(categories, data, classIndex));

至:

promise = promise.then(doSomethingAndReturnAPromise.bind(this,categories, data, index));

或者一个完全成熟的匿名函数:

promise = promise.then(function(){
    return doSomethingAndReturnAPromise(categories, data, classIndex));
});

作为一个侧面说明,如果你需要解决与类别的承诺,这将是简单的改变返回值,而不是直接把该支票在foreach。

processCategories = function(categories) {
    return categories.reduce(function (prev, data, index) {
            return prev.then(function(){
                return doSomethingAndReturnAPromise(categories, data, index));
            });
    }, $q.when()).then(function(){
        return categories;
    });
}


Answer 2:

更改processCategories()来是这样的:

processCategories = function(categories) {
    var promise, deferred;

    // Process each category in sequence
    angular.forEach(categories, function (data, classIndex) {
      if(promise) {
        promise = promise.then(function() {
           return doSomethingAndReturnAPromise(categories, data, classIndex);
        });
      } else
        promise = doSomethingAndReturnAPromise(categories, data, classIndex);
    });

    if(promise) {
      promise.then(function() {
        return categories;
      });
    } else {
      deferred = $q.defer();
      promise = deferred.promise;
      deferred.resolve(categories);
    }


    // Pass the last promise
    return promise;
}


文章来源: Chained promises in Angular