你如何处理解析承诺错误,以便他们不继续执行代码?

时间:2014-09-15 18:24:03

标签: javascript parse-platform promise

假设我有一个Parse Cloud Code js函数,我希望返回一个承诺,例如:

function doSomething(myObj, abortIfSaveFails) {
  var dsPromise = new Parse.Promise();
  myObj.set("name", "abc");
  myObj.save().then(function(){
    // location "A"
    // great, it worked!
    // don't want to actually do anything more in this block because 
    // we might want to do the same things even if save fails, and I 
    // don't want to repeat code
    return Parse.Promise.as();
  }, function(error){
    // location "B"
    // save failed, but we might want to keep going
    if (abortIfSaveFails) {
      // location "C": wish I could abort the whole promise chain here!
      return Parse.Promise.error();
    } else {
      return Parse.Promise.as();
    }
  }).then(function(){
    // location "D"
    // at this point we're not sure if save succeeded but let's 
    // assume we don't need to know
    return doSomethingCruciallyImportantAndReturnAPromise();
  }, function(error){  
    // location "E": 
    // not sure if we got here because doSomethingCruciallyImportant...() errored or 
    // because myObj.save() errored.
    // would be nice to abort the whole thing right now!
    return Parse.Promise.error();
  }).then(function(){
    // location "F"
    // at this point we know doSomethingElse... succeeded
    return doSomethingUnimportantAndReturnAPromise();
  }, function(error){
    // location "G"
    // not sure if we got here because doSomethingCruciallyImportant...() errored or
    // because doSomethingUnimportant...() errored.
    // If doSomethingCruciallyImportant...() succeeded but doSomethingUnimportant...()
    // failed, I'd LIKE to do dsPromise.resolve()...  but I can't resolve, because 
    // we might be in the process of aborting the function because myObj.save() rejected,
    // or doSomethingCruciallyImportant rejected!
    dsPromise.reject(); // might not be what I want to do!
  }).then(function(){
    // location "H"
    // everything worked fine
    dsPromise.resolve();
  });
  // location "I"
  return dsPromise; // return the promise so `then` will wait
}

如何重构/重写它以更好地处理C,E和G位置的情况?

我意识到我可以在C和E处使用dsPromise.reject(),但是当前正在执行的promise链会发生什么?它不会继续执行并继续前进到D,E,F,G等?然后,我无法到达我多次解析dsPromise的地方?

1 个答案:

答案 0 :(得分:4)

  

如何重构/重写它以更好地处理位置C,E和G的情况?

适当地嵌套处理程序。如果一个处理程序应该只处理一个动作的解决方案,那么直接用.then将它与该动作的承诺链接起来,而不是链中的其他地方。

在位置E中,您不会将处理程序附加到包含保存调用的链,而只会附加到onFulfilled(即未中止)分支中的承诺。

doSomethingCruciallyImportant().then(function(){
    // location "F"
    // at this point we know doSomethingCruciallyImportant succeeded
    return doSomethingUnimportant();
}, function(error){
    // location "G"
    // not sure if we got here because doSomethingCruciallyImportant() errored
    // or because doSomethingUnimportant() errored.
});

没有。关于onRejection的{​​{1}}处理程序如何实际工作的Read more - 执行.then()处理程序时调用 (调用onFulfilled }} 这里)。在位置G中,您确切知道在doSomethingUnimportant()调用之前链中的某些内容失败了 - then在我的简化代码段中。

组合:

doSomethingCruciallyImportant()