3
我正試圖找到一種方式來打破AngularJS代碼中的承諾鏈。顯而易見的方法是返回一個對象,然後檢查鏈中每個「then」函數的有效性。突破當時在Angularjs中的承諾
我想找到一個更優雅的方式打破一個然後鏈。
我正試圖找到一種方式來打破AngularJS代碼中的承諾鏈。顯而易見的方法是返回一個對象,然後檢查鏈中每個「then」函數的有效性。突破當時在Angularjs中的承諾
我想找到一個更優雅的方式打破一個然後鏈。
在角度上,可以在指令,控制器等中注入$ q服務,這是Kris Kowal的問題的一個緊密實現。 因此,在函數內部而不是返回值或其他東西鏈接到下一個 「thenable」 功能,只返回一個$q.reject('reject reason');
例子:
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'
});
}]);