根据blog post,可以在解析器函数中返回 null
以跳过promise链中的后续 then
处理程序 . 我很好奇它是如何工作的 - Promises/A+ spec中的任何内容都没有定义我能看到的这种行为 .
对于我的用例,我调整了他们的示例来处理Angular UI Bootstrap中$uibModal模态对话服务的拒绝,该服务根据模态是关闭还是解除来解析或拒绝:
$uibModal.open({
// ... modal options
})
.result
.catch(function () {
// Modal dialog was dismissed, so use this trick to stop the promise chain
return $q(function () { return null; });
})
.then(function () {
// ... this will not get executed if dialog was dismissed
return someAPIcall();
})
.catch(function () {
// ... handle errors with API call
});
这很好,因为我不必在第一个 catch
中检查错误值( $uibModal
API使用各种字符串作为模态拒绝类型),或者使用复杂的分支/嵌套来避免将模态解雇拒绝的处理与 someAPIcall()
产生的错误混合在一起 .
但它是如何工作的?这记录在哪里?