If I have long chain of promises where there might be some logic provided by the back-end that something is not right. For example I try to put cars in garages. Some garages can't hold trucks. Some other garages are made large enough to hold trucks so we don't want to put regular cars there. And procedure example:
- I try put 10 trucks in a 2-car garage
- Some calls are made to determine specifications of vehicles and garages involved.
- Midway through the promise chain I get the info from back-end that it's not acceptable. "You are trying to stuff too many vehicles", "Vehicle 1 is a truck", ... "Vehicle 10 is a truck."
- Now I would normally request saving of data, but atm there are violations triggered so I pass a promise rejection through the n steps of promise chain.
Can I somehow just ignore the rest of the chain and do
somethingIsWrong(violations);
instead of going through the promise chain while rejecting all.
var promise = firstCall();
promise.then( function(){
secondCall();
}, error, notify
)
.then(
function(params) {
var def = $q.defer();
return getViolations(params)
.then(
function(violations){
if(violations.length==0){
//OK
def.resolve(params);
} else {
return $q.reject("violations triggered");
// ideally here would be this:
//somethingIsWrong(violations);
}
return def.promise;
},
function(errMsg) { // errorCallback
return $q.reject(errMsg);
},
function() { /*notifyCallback*/ }
)
},
function(errMsg) { // errorCallback
return $q.reject(errMsg);
},
function() { /*notifyCallback*/ }
)
// Here may be n number of .then() calls while resolving all necessary data of cars and garages.
.then(
function(updateset) {
log("YEAH SUCCESS");
},
function(errMsg) { // errorCallback
//For now we deal this with a pop-up, but possible will do some thing else
window.alert('Violations triggered:\n\n' + errMsg);
},
function() { /*notifyCallback */ }
);
---EDIT---
In the end I ended up with something like this:
var promise = firstCall();
promise.then( function(){
secondCall();
}, error
)
.then(
function(params) {
return getViolations(params)
.then(
function(violations){
if(violations.length==0){
//OK
return restOfTheSuccessfulRun(params);
} else {
return $q.reject("violations triggered");
// ideally here would be this:
return somethingIsWrong(violations);
}
},
function(errMsg) { // errorCallback
return $q.reject(errMsg);
}
)
},
function(errMsg) { // errorCallback
return $q.reject(errMsg);
}
);
function restOfTheSuccessfulRun(params) {
asyncCallsNTimes(params)
// Here may be n number of .then() calls while resolving all necessary data of cars and garages.
.then(
function(updateset) {
log("YEAH SUCCESS");
},
function(errMsg) { // errorCallback
//For now we deal this with a pop-up, but possible will do some thing else
window.alert('Violations triggered:\n\n' + errMsg);
}
)
}
function somethingIsWrong(violations) {
var response;
//handle violations
return response;
}
Also I want to add that you have a badly designed rest api if you need to call it 5 times to perform one action. But just in case you can't influence it anymore then this might be a good way. In other words when half way through your promise chain you need to make a decision based on one of the responses that how you will continue to the end. And both directions are valid (not regular error/promise-error handling).