Using Redirections Into Promises Chain
I'm using promises to call a serie of webservices, but I'd like, in some cases, use redirection in the middle of my logic. The problem is that the promises chain continue to execut
Solution 1:
func = function () {
return PromiseCall1().then(function (data) {
if (condition) {
$location.path(some_url); // This is a redirection with AngularJS
} else {
return PromiseCall2().then(function (data) {
return PromiseCall3();
}).then(function (data) {
// Some other stuff here
});
}
});
};
Solution 2:
Petka's solution is ok. If you want. It is possible if you wrap your then handlers with Maybe.
Something like:
var maybe = function(fn){
return function(value){
if(value === maybe.Nothing) return maybe.Nothing;
return fn.apply(this,arguments);
};
});
maybe.Nothing = {}; // unique reference
Which would let you do
Promise.try(function(){
return15;
}).then(maybe(function(val){
console.log("got",val);
return maybe.Nothing;
})).then(maybe(function(val){
console.log("This never gets called");
}));
This gives you an explicit value hook to signal not continuing any promise in the chain that is wrapped with a maybe.
Post a Comment for "Using Redirections Into Promises Chain"