Javascript 承诺没有按顺序运行

Javascript 承诺没有按顺序运行,javascript,angularjs,Javascript,Angularjs,我的控制器中有一个功能,可以将用户添加到组中,一旦用户被分配到组中,可用组的列表就会减少。我尝试在函数中使用承诺来控制流,但根据控制台日志,我的所有groupServices首先运行,然后userServices随后运行,从而阻止了要更新的可用组列表 控制器功能: $scope.addUserToGroup = function (){ var defer = $q.defer(); defer.promise.then(function (){

我的控制器中有一个功能,可以将用户添加到组中,一旦用户被分配到组中,可用组的列表就会减少。我尝试在函数中使用承诺来控制流,但根据控制台日志,我的所有groupServices首先运行,然后userServices随后运行,从而阻止了要更新的可用组列表

控制器功能:

    $scope.addUserToGroup = function (){
        var defer = $q.defer();
        defer.promise.then(function (){
            userService.addUserToGroup(
                $scope.selectedUser, 
                $scope.selectedAvailableGroups, 
                $scope.assignedGroups, 
                $scope.availableGroups,
                $scope.groups
            ); 
        }).then(compare());
        defer.resolve();
    };
    function compare(){
    console.log('comparing assigned with all ');
        $scope.compareGroups = groupService.compareGroups();         
    }
我使用承诺来确保事情按顺序运行,但根据我的控制台输出,情况似乎并非如此

用户服务功能

  var addUserToGroup = function (selectedUser, selectedAvailableGroups, assignedGroups, availableGroups, groups){

    console.dir(selectedUser);
    console.dir(selectedAvailableGroups);
    console.dir(assignedGroups);
    console.dir(availableGroups);
    console.dir(groups);

    var deferred = $q.defer();

    var addPromise = [];
    var selectLength = selectedAvailableGroups.length;

    //Add user to selected groups on server
    deferred.promise
      .then(function (){ 
        for (var i = 0; i < selectLength; i++){
          addPromise[i] = $().SPServices({
            operation: "AddUserToGroup",
            groupName: selectedAvailableGroups[i].name,
            userLoginName: selectedUser.domain
          });      
        }; 
      })
      .then(function (){
          //when promise finished, push changes to availableGroups
          for (var i = 0; i < selectLength; i++){
            assignedGroups.push(selectedAvailableGroups[i]);
            console.log(selectedUser.name + " added to: " + selectedAvailableGroups[i].name);
          };
      });
    //Run
    deferred.resolve();
  }
控制台日志:

comparing assigned with all  userCtrl.js:47
assigned groups             groupServices.js:63
Array[8]                    groupServices.js:64
available gruops            groupServices.js:65
Array[3]                    groupServices.js:82
Array[3]                    groupServices.js:83
Object                      userServices.js:38
Array[1]                    userServices.js:39
Array[8]                    userServices.js:40
Array[4]                    userServices.js:41
Array[11]                   userServices.js:42
User added to: Test Group 4 userServices.js:64

你以错误的方式使用承诺

第一个问题是:

然后(compare())

您试图将执行
compare
函数的结果注册为回调,而不是像这样只注册
compare
函数:

然后(比较)

这就是为什么它首先执行,然后调用
groupService.compareGroups()
,只有在它完成后,您才调用
defer.resolve()
,并执行第一个注册回调。这就是您看到当前控制台输出的原因

您需要按照以下方式修改用户服务和控制器以使其工作(使用服务与承诺一起工作):

控制器功能:

function compare(){
    console.log('comparing assigned with all ');
    $scope.compareGroups = groupService.compareGroups();         
}

$scope.addUserToGroup = function (){
    userService.addUserToGroup(
        $scope.selectedUser, 
        $scope.selectedAvailableGroups, 
        $scope.assignedGroups, 
        $scope.availableGroups,
        $scope.groups
    ).then(compare); 
};
用户服务功能:

function compare(){
    console.log('comparing assigned with all ');
    $scope.compareGroups = groupService.compareGroups();         
}

$scope.addUserToGroup = function (){
    userService.addUserToGroup(
        $scope.selectedUser, 
        $scope.selectedAvailableGroups, 
        $scope.assignedGroups, 
        $scope.availableGroups,
        $scope.groups
    ).then(compare); 
};
(假设
$().SPServices
返回承诺)

var addUserToGroup=函数(selectedUser、selectedAvailableGroups、assignedGroups、availableGroups、groups){
console.dir(selectedUser);
console.dir(selectedAvailableGroups);
console.dir(assignedGroups);
console.dir(可用组);
console.dir(组);
var deferred=$q.deferred();
var addPromise=[];
var selectLength=selectedAvailableGroups.length;
//将用户添加到服务器上的选定组
对于(变量i=0;i
Hi Andrey,我已经实现了您的更改,但现在整个过程比以前慢了很多。另外,当比较运行并调整可用组时,现在返回正确的组数,但DOM不会更新以反映更改。@蝙蝠侠您使用的是哪个版本的AngularJs和Promissions Api实现?你能提供你实现的东西吗?我会把我的项目推到github上。应该给你一个完整的画面。给我一点时间。@Batman什么东西在DOM中没有更新?如果您的意思是
$scope.compareGroups=groupService.compareGroups(),那么这是因为
groupService.compareGroups()不返回任何内容。我可以使用您建议的承诺使其正常工作。谢谢你的帮助。
var addUserToGroup = function (selectedUser, selectedAvailableGroups, assignedGroups, availableGroups, groups){

    console.dir(selectedUser);
    console.dir(selectedAvailableGroups);
    console.dir(assignedGroups);
    console.dir(availableGroups);
    console.dir(groups);

    var deferred = $q.defer();

    var addPromise = [];
    var selectLength = selectedAvailableGroups.length;

    //Add user to selected groups on server
    for (var i = 0; i < selectLength; i++){
        addPromise[i] = $().SPServices({
            operation: "AddUserToGroup",
            groupName: selectedAvailableGroups[i].name,
            userLoginName: selectedUser.domain
        });      
    }

    $q.all(addPromise).then(function (){
        //when promise finished, push changes to availableGroups
        for (var i = 0; i < selectLength; i++){
            assignedGroups.push(selectedAvailableGroups[i]);
            console.log(selectedUser.name + " added to: " + selectedAvailableGroups[i].name);
        };

        //Finish function
        deferred.resolve();
    });
    return deferred.promise;
}