How/can do you chain promises for firebase transactions inside a for loop

Viewed 1901

I am looking to increment several counters with Firebase transactions and also add set a new value in firebase. The key being I want to make sure it is all successful or not save any of the data.

I imagine this would be best done with promises? Unfortunately I'm not very familiar with them

So far I have:

$scope.addNewPost = function() {
  var ref = new Firebase(FBURL).child('/posts').push();
  var onComplete = function(error) {
    if (error) {
      alert('Error: Something went wrong when creating your post please try again');
      throw new Error(error);
    } else {
      $scope.reset(); // or redirect to post
    }
  };
  var tags = $scope.post.tags.split(', ');
  console.log(tags);
  angular.forEach(tags, function(value, index){
    var refTag = new Firebase(FBURL).child('/tags/' + value);
    refTag.transaction(function(current_value) {
      return current_value + 1;
    });
  });
  ref.set($scope.post, onComplete);
};

Could anyone point me towards some references on how I can achieve this or offer advice? Perhaps there is an better Angularfire way to do this?

Moved follow up question into a new question dealing specifically with error handling

1 Answers
Related