用Angularjs打破当时的承诺
我试图找到一种方法来打破AngularJS代码中的承诺链。最明显的方法是返回一个对象,然后检查链中每个“then”函数的有效性用Angularjs打破当时的承诺,angularjs,break,q,angular-promise,Angularjs,Break,Q,Angular Promise,我试图找到一种方法来打破AngularJS代码中的承诺链。最明显的方法是返回一个对象,然后检查链中每个“then”函数的有效性 我想找到一种更优雅的方式来打破当时的链条。在angular中,有$q服务可以注入指令、控制器等,这是Kris Kowal的q的一个近似实现。 因此,在then函数内部,只返回$q.reject('reject reason'),而不是返回将链接到下一个“thenable”函数的值或其他内容 例如: angular.module('myQmodule',[]) .cont
我想找到一种更优雅的方式来打破当时的链条。在angular中,有$q服务可以注入指令、控制器等,这是Kris Kowal的q的一个近似实现。 因此,在then函数内部,只返回
$q.reject('reject reason'),而不是返回将链接到下一个“thenable”函数的值或其他内容代码>
例如:
angular.module('myQmodule',[])
.controller('exController',['$q',function($q){
//here we suppose that we have a promise-like function promiseFunction()
promiseFunction().then(function(result1){
//do the check we want in order to end chain
if (endChainCheck) {
return $q.reject('give a reason');
}
return;
})
.then(function(){
//this will never be entered if we return the rejected $q
})
.catch(function(error){
//this will be entered if we returned the rejected $q with error = 'give a reason'
});
}]);