如何以角度连续顺序运行异步

时间:2017-07-12 13:20:01

标签: javascript angularjs

 $scope.SaveAuditItems = function (audit) {
    var isError = false;
    $scope.isProcessing = true;
    var defer = $q.defer();
    var promises = [];

    for (var siteCounter = 0; siteCounter < audit.SessionSiteList.length; siteCounter++) {
        for (var itemCounter = 0; itemCounter < audit.SessionSiteList[siteCounter].AuditItemList.length; itemCounter++) {
            var item = audit.SessionSiteList[siteCounter].AuditItemList[itemCounter];

            item.TotalItems = audit.SessionSiteList[siteCounter].AuditItemList.length;
            item.CurrentItem = itemCounter;

            promises.push($scope.submitaudit(item));
        }
    };
     //It appears to be running all the promises in the array in parrallel then running CloseAudit.
        $q.all(promises).then(
            function (response) {
                $scope.CloseAudit(audit.AuditSessionId).then(function () {
            },
            function(){
                alert("done");
            },
            function(){
                alert("done");
            }
            );
        }).catch(function (exception) {
            $scope.Loading("Could not submit audit session #'+ audit.AuditSessionId +' , please try again.", 2000);
        });


}

如何使承诺按连续顺序运行?它会导致服务器数据的竞争条件被提交给?这个有角度的1代码。

当我不知道将按顺序运行多少承诺时,我如何使用呢?所有其他答案都使用,但始终是预定义的承诺数。我不可能在一个永恒的时间内......我似乎无法理解我是如何做到这一点的。

------------------------------------- edit 2 -------- -------------------------------------

  $scope.SaveAuditItems = function (audit) {
    $ionicLoading.show({
        template: '<i class="icon ion-loading-c"></i>Please wait..Sending Item ( 1 Of ' + $scope.AuditItemCounter + ' )',
    }).then(function () {});
    var isError = false;
    $scope.isProcessing = true;
    var defer = $q.defer();
    var promises = [];

    for (var siteCounter = 0; siteCounter < audit.SessionSiteList.length; siteCounter++) {
        for (var itemCounter = 0; itemCounter < audit.SessionSiteList[siteCounter].AuditItemList.length; itemCounter++) {
            var item = audit.SessionSiteList[siteCounter].AuditItemList[itemCounter];

            item.TotalItems = audit.SessionSiteList[siteCounter].AuditItemList.length;
            item.CurrentItem = itemCounter;
            // $scope.Loading('Sending Item ( ' + item.CurrentItem + ' Of ' + item.TotalItems + ' )..', 0).then(function () {

            promises.push($scope.submitaudit(item));



            ConsoleLogger.AddLog('Sent Item ( ' + item.CurrentItem + ' Of ' + item.TotalItems + ' )');

            //Loading.show({ template: '<i class="icon ion-loading-c"></i>Sending Item ' + item.CurrentItem + ' of ' + item.TotalItems + ' ', }).then(function () { });
            $scope.Loading('Sent item ( ' + item.CurrentItem + ' Of ' + item.TotalItems + ' )', 0).then(function () {});


        }
    }

    var all = promises.reduce(function (cur, next) {
        return cur.then(next);
    }, Promise.resolve(true));


    all.then(function (a) {
        $ionicLoading.show({
            template: '<i class="icon ion-loading-c"></i>Finalising your audit..'
        }).then(function () {
                $scope.CloseAudit(audit.AuditSessionId).then(function () {
                    ConsoleLogger.AddLog('Audit submitted successfully.');
                });
            },
            function () {
                alert("doneeee");
            },
            function () {
                alert("done");
            }
        );
    });

enter image description here

您可以在时间上看到承诺未按预期顺序运行?我错过了什么?我做了承诺,超时7秒.. 在所有承诺退回但我没有发生后,closeaudit应该已经运行了!

通过CHNAGOT工作           promises.push($ scope.submitaudit(项目)); TO

 promises.push(function(){return $scope.submitaudit(item)});

1 个答案:

答案 0 :(得分:1)

这是一个如何做的快速示例。逐个减少数组中的所有promise,reduce函数将链接每个promise的then()。它将在第一个已经解决后首先开始,它将调用第二个,依此类推......到最后;]。

&#13;
&#13;
var promises = [
	function() { return new Promise(function(resolve, reject) {
  	setTimeout(function() {
    	console.log('resolve promise 1 after 3sec');
    	resolve('promise 1');
    }, 3000)
  })},
  function() { return new Promise(function(resolve, reject) {
  	setTimeout(function() {
    	console.log('resolve promise 2 after 1.5sec');
    	resolve('promise 2');
    }, 1500)
  })},
  function() { return new Promise(function(resolve, reject) {
  	setTimeout(function() {
    	console.log('resolve promise 3 after 2sec');
    	resolve('promise 3');
    }, 2000);
  })}];

var all = promises.reduce(function(cur, next) {
    return cur.then(next);
}, Promise.resolve(true));


all.then(function(a) {
    console.log('all are done!');
});
&#13;
&#13;
&#13;