为什么成功承诺的链式回调会在成功和失败时同时执行?



所以我有一个保存Ember数据模型的基本函数,并在失败时运行通用回调代码,然后将承诺返回给调用方,因此调用方可以自己链接其他回调,但不知何故,来自调用方的链接回调没有按预期执行,成功代码在失败和成功时执行,而失败代码从未执行

下面是通用函数的代码:
// this function returns the model.save() promise to allow chaining of promises on the caller scope.

   function baseCall() {
      return model.save().then(function () {
        //success
      }, function (reason) {
        //failure
        if (reason.errors) {
          model.set('errors', reason.errors);
        }
      });
    }  

来电者代码:

baseCall().then(function(post) {
        console.log('super.success');//runs on failure AND success
      }, function() {
        console.log('super.failure');//never runs
      });

出于同样的原因,下面的代码总是警告"Hello";

try {
     model.save(); // throws Error
} catch (e) {
    if (reason.errors) {
      model.set('errors', reason.errors);
    }
}
console.log("Hello");

承诺错误处理程序,如同步catch"处理"错误。如果您不想将错误标记为已处理-重新抛出它:

 throw reason; // always throw proper `Error`s

最新更新