0

I have a very simple JavaScript chain, much simpler than the previous similar questions' code: a, b, c. So I am wondering perhaps there might be a simpler answer as well.

Here is my promise chain, with Mongoose.

var someValue = 314;

// 1st promise
var promiseStep1 = step1.find({}).exec(); 
promiseStep1.then(function(doc1){
  // do stuff with doc1

  if (doc1 == someValue){
     // HOW DO I BREAK OUT OF THE PROMISE CHAIN 
     // and not execute the 2nd and 3rd promises below?
  }

  // 2nd promise
  return step2.find({}).exec();
}).then(function(doc2){
  // do stuff with doc2

  // 3rd promise
  return step3.find({}).exec();
}).then(function(doc3){
  // do stuff with doc3

  // we're all done here
  // handles error for all the promises
}, function(err){
  return err;
});

How do I break out of the promise chain in the if (doc1 == someValue) condition so that the latter promises don't get executed?

Community
  • 1
  • 1
Melissa
  • 1,236
  • 5
  • 12
  • 29
  • Too bad Javascript doesn't have "goto" ;) One other interesting link: http://stackoverflow.com/questions/29478751/how-to-cancel-an-emcascript6-vanilla-javascript-promise-chain – paulsm4 Dec 22 '15 at 03:55
  • You can break out of a promise chain by rejecting the promise. So for example, `return Promise.reject(new Error('Too bad so sad'))` – Steve Campbell Dec 22 '15 at 16:00

0 Answers0