0

How to stop Promise.then execution without .catch function execution?

Promise
.resolve("a")
.then(function(a){
    console.log("step1 = " + a);
    return a;
})
.then(function(a){
    console.log("step2 = " + a);
    if (!someReason) {                
        return Promise.reject(); // I whant to stop flow execution here
    }        
})
.then(function(a){
    console.log("step3 = " + a);
    return a;
})
.catch(function(err){
// I don't whant to have .catch() executed in case of planned stop
   console.log("Flow failed");
});
Alexey Ryazhskikh
  • 2,458
  • 4
  • 32
  • 51
  • Your code works fines, I never see console.log("step3 = " + a) output, but promise is left pending. – An0nC0d3r Oct 25 '15 at 09:37
  • Silly question but you never know who knows what sooo... what do you mean by "want to stop flow execution here".... specifically. – An0nC0d3r Oct 25 '15 at 09:45
  • My sample executes function in .catch(), I want to avoid it. So question is more about symantic. For example, I don't whant to call catch() for case if I got data from cache and I don't want to call next .thens to get data from database. – Alexey Ryazhskikh Oct 25 '15 at 10:07
  • You need to refactor your code with a different logic flow, do you know how? – An0nC0d3r Oct 25 '15 at 10:20
  • I'm just looking something like Promise.rejectWithoutCatchCall() or another way to make code readable and consistent. As I understand, I can return step3 promise in step2, but in will be nested. Currently I'm trying to avoid nesting by promises. – Alexey Ryazhskikh Oct 25 '15 at 10:27
  • @musuk: Branching (which is what you want here) always comes with nesting (if you don't resort to `goto`). Just do it. – Bergi Oct 25 '15 at 10:38

0 Answers0