只显示一条消息后用promise保证。然后AngularJS

问题描述:

你好家伙我在AngularJS中使用REST在确认按钮中调用此函数。保存没问题,问题发生在成功或错误消息中。如果成功执行循环,它应该只显示一条成功消息,或者不止一次显示任何错误的错误消息。如果发生错误,我使用了一个标志isError,但是不管我做什么,“if(isError == false){”总是在循环结束前执行。只显示一条消息后用promise保证。然后AngularJS

$scope.confirm = function() { 
     var isError = false; 
     var i=0; 
     $scope.displayedmodalcollection.forEach(function(item) { 
      i++; 
      delete item.added; 
      result = DataBase.update({ 
       id: item._id, 
       endpoint: item._type 
      }, item._source).$promise.then(function(response) { 

      }, function(err) { 
       isError = true; 
       if (err.status === 500) { 
        ngToast.create({ 
         className: 'warning', 
         content: String.format('Any message.', item._id) 
        }); 
       } 
       else if (err.status === 404) { 
        ngToast.create({ 
         className: 'danger', 
         content: '404 - other message' 
        }); 
       } else { 
        ngToast.create({ 
         className: 'danger', 
         content: 'Error' 
        }); 
       } 
      }); 
     }); 
     if (isError == false){ 

      ngToast.create({ 
       className: 'success', 
       content: 'Success' 
      }); 
     } 
    }; 

有没有人有建议呢? 感谢

你可以使用$q.all()显示解决

而不是基于所有更新的成功举杯:

$scope.displayedmodalcollection.forEach(function(item) { 

这样做

// map array of update promises 
var updatePromises = $scope.displayedmodalcollection.map(function(item){ 
    // return the promise 
    return DataBase.update({...}).$promise.then(function(response) { 
       // success code 
      }, function(err) { 
       // error code 
      });  
});  

$q.all(updatePromises).then(function(){ 
     // all promises were resolved 
     ngToast.create({ 
       className: 'success', 
       content: 'Success' 
     }); 
}); 

记住注入$ Q in controller

+0

这并不确保成功敬酒发生在年底的循环虽然。 –

+0

@JeremyJackson啊对..我没有注意到循环 – charlietfl

我不知道你想怎么处理成功的更新和错误,但这应该把你在正确的轨道上:

$scope.confirm = function() { 
    var successes = 0; 
    var errors = 0; 
    $scope.displayedmodalcollection.forEach(function(item) { 
     delete item.added; 
     result = DataBase.update({ 
      id: item._id, 
      endpoint: item._type 
     }, item._source).$promise.then(function(response) { 
      successes++; 
      checkStatus(successes, errors); 
     }, function(err) { 
      errors++; 
      checkStatus(successes, errors); 
      if (err.status === 500) { 
       ngToast.create({ 
        className: 'warning', 
        content: String.format('Any message.', item._id) 
       }); 
      } else if (err.status === 404) { 
       ngToast.create({ 
        className: 'danger', 
        content: '404 - other message' 
       }); 
      } else { 
       ngToast.create({ 
        className: 'danger', 
        content: 'Error' 
       }); 
      } 
     }); 
    }); 
}; 

function checkStatus(successes, errors) { 
    var isCompleted = (successes + errors) == $scope.displayedmodalcollection.length; 
    var notAllFailed = successes > 0; 
    if (isCompleted && notAllFailed) { 
     handleSuccessToast(); 
    } 
} 

function handleSuccessToast() { 
    ngToast.create({ 
     className: 'success', 
     content: 'Success' 
    }); 
} 

的是,在isError == false分支的成功逻辑被所有的之前执行的问题数据库调用已完成。因此,它在错误实际发生之前可能会运行,因此该标志是错误的,并且遵循分支。

发生这种情况是因为DB逻辑正在返回承诺。 Promise不会阻止执行其他代码 - 它们只是推迟'then'回调中包含的逻辑,直到DB调用实际完成。因此,如果您的收藏中有5件商品,则可以在实际完成其中任何一件商品之前启动所有5件商品的数据库调用。关键是你需要让你的成功逻辑等待,直到你知道所有数据库承诺已经解决,并且所有的调用都完成。有关承诺的更多信息,请参阅this Promise guide

这里是一个如何编写你的逻辑等待所有承诺的例子,你检查之前,如果任何错误发生到解决:

var doneCount = 0; 
var isError = false; 
something.foreach(item) { 
    somedblogic(item).$promise.then(function (response) { 
     if (++doneCount === something.length && !isError) { 
      //-- success logic here - runs one time after all db calls complete 
     } 
    },function (err) { 
     doneCount++; 
     isError = true; 
     //-- rest of error logic here - runs once for each error returned 
    }); 
} 
+0

谢谢@grumpyhoser,它真的解决了这个问题:D –