如何回滚更改时,有一个承诺链中的错误 [英] How to roll back changes when there is an error in a promise chain

查看:137
本文介绍了如何回滚更改时,有一个承诺链中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的角度应用程序我想更改几个地点在我火力与交易和设定的组合。我已经写了承诺链<一个href=\"http://stackoverflow.com/questions/20848684/how-can-do-you-chain-promises-for-firebase-transactions-inside-a-for-loop\">a一点帮助。现在我需要处理可能发生的任何错误。

在任何的承诺的一个错误的情况下,我希望回滚的火力点(成功的承诺)所做的任何更改,并提醒用户故障。

当前code以下

  $ scope.addNewPost =功能(){
  。VAR refPosts =新的火力地堡(FBURL).child('/帖),推();
  //获取广告代码到数组递增计数器
  变种标记= $ scope.post.tags.split(,);
  变种allPromises = [];
  //通过标签迭代,并设置应许交易递增标记计数
  angular.forEach(标签,功能(价值指数){
    变种DFD = $ q.defer();
    VAR参考标签=新的火力地堡(FBURL).child('/标签/+值);
    refTag.transaction(功能(current_value){
      返回current_value + 1;
    },功能(错误,提交快照){
      如果(提交){
        dfd.resolve(快照);
      }其他{
        dfd.reject(错误);
      }
    });
    allPromises.push(dfd.promise);
  });  //添加诺言设置POST数据
  变种DFD = $ q.defer();
  refPosts.set($ scope.post,功能(错误){
    如果(错误){
      dfd.reject(错误);
    }其他{
      dfd.resolve('后录);
    }
  });
  allPromises.push(dfd.promise);  $ q.all(allPromises)。然后(
    功能(){
      $ scope.reset(); //或重定向发布
    },
    功能(错误){
      //错误处理到这里我会怎样
      //回滚写入火力任何数据
      警报('错误:出事了你的帖子没有被创建。');
    }
  );
};

因此​​,我需要知道的是我怎么回滚发生在我的火力点数据,如果这些承诺之一失败的任何变化。有可能是任何数量的火力发生更新。 (例如:通过交易递增3个标签和后数据被设置)

我怎么会写失败函数来计算什么是成功的,并撤消了吗?如果这是这甚至可能。

---------------从原来的职位子的问题已经解决---------------

另外你怎么用力错误?我试过设置像下面的变量,但它似乎没有工作,是有什么错我的。然后?

  refPosts.set($ scope.post,功能(错误){
  VAR forceError = TRUE;
  如果(forceError){
    dfd.reject(forceError);
  }其他{
    dfd.resolve('后录);
  }
  allPromises.push(dfd.promise);
});


解决方案

有这一行的两个实例,并且它们都在错误的位置:

  allPromises.push(dfd.promise);


  • 在第一个块,应该是在最后一个语句在foreach回调,而不是在事务回调。

  • 在第二块,应该是在调用设置(),而不是回调。
  • 之后

您code现在写入的方式, $ q.all()越来越承诺的空数组。这也可能是做什么用你在尝试 forceError 测试的干扰。

In my angular app I want to make changes to several locations in my firebase with a mix of transactions and set. I have written a promise chain with a little help. Now I need to handle any errors that may occur.

In the event of an error on any of the promises I would want to roll back any changes made in firebase (the successful promises) and alert the user to the failure.

Current code below

$scope.addNewPost = function() {
  var refPosts = new Firebase(FBURL).child('/posts').push();
  // Get tags into array for incrementing counters
  var tags = $scope.post.tags.split(', ');
  var allPromises = [];
  // Iterate through tags and set promises for transactions to increment tag count
  angular.forEach(tags, function(value, index){
    var dfd = $q.defer();
    var refTag = new Firebase(FBURL).child('/tags/' + value);
    refTag.transaction( function (current_value) {
      return current_value + 1;
    }, function(error, committed, snapshot) {
      if (committed) {
        dfd.resolve( snapshot );
      } else {
        dfd.reject( error );
      }
    });
    allPromises.push( dfd.promise );
  });

  // Add promise for setting the post data
  var dfd = $q.defer();
  refPosts.set( $scope.post, function (error) {
    if (error) {
      dfd.reject(error);
    } else {
      dfd.resolve('post recorded');
    }
  });
  allPromises.push( dfd.promise );

  $q.all( allPromises ).then(
    function () {
      $scope.reset(); // or redirect to post
    },
    function (error) {
      // error handling goes here how would I
      // roll back any data written to firebase
      alert('Error: something went wrong your post has not been created.');
    }
  );
};

So what I need to know is how do I roll back any changes that happen to my firebase data in the event that one of these promises fail. There could be any number of updates happening in firebase. (for example: 3 tags being incremented via transaction and the post data being set)

How would I write the failure function to calculate what was successful and undo it? If this is this even possible.

--------------- sub question from original post has been solved ---------------

Also how do you force errors? I've tried setting a variable like below but it doesn't seem to work, is there something wrong with my .then?

refPosts.set( $scope.post, function (error) {
  var forceError = true;
  if (forceError) {
    dfd.reject(forceError);
  } else {
    dfd.resolve('post recorded');
  }
  allPromises.push( dfd.promise );
});

解决方案

There are two instances of this line, and they are both in the wrong place:

allPromises.push( dfd.promise );

  • In the first block, it should be in the last statement in the forEach callback, not in the transaction callback.
  • In the second block, it should be after the call to set(), not in the callback.

The way your code is written now, $q.all() is getting an empty array of promises. That could also be what's interfering with the forceError test you're attempting.

这篇关于如何回滚更改时,有一个承诺链中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆