1

I am new to UnhandledRejection. The below method is supposed to throw exception and terminate flow but it doesn't.Please favour on resolving it either way

Case 1: Took value from promise as true and tried conditions.But it is bypassed and returns unhandled rejection with the exception to be thrown.

Utils.isMaximumLimitReached(id).then(isLimit=>{
    console.log(isLimit); //true
    if(isLimit){
       throw "not allowed";
    }
  })

Edit: Case 3:This too returns Unhandled rejection this is not allowed

const isMaximumLimitReached = coachingId => {
  return db.coachingClassEntries
    .findAndCountAll()
    .then(counts => {
      let numberOfEntries = 2;
      //let maxEntries = counts.rows[0].coachingClass.maxEntries;
      let maxEntries=2;
      return new Promise((resolve,reject)=>{
        if (numberOfEntries == maxEntries) {
           reject('this is not allowed');
      }
      });

    });
};

  Utils.isMaximumLimitReached(data.coachingClassId).then().catch(error=>{
       throw error;
   })
Gayathri
  • 1,776
  • 5
  • 23
  • 50
  • Check This https://stackoverflow.com/questions/40500490/what-is-an-unhandled-promise-rejection . You need to add the catch block to handle every promise . – Rohan Kadu Feb 16 '18 at 10:58
  • @RohanKadu even with catch block, it throws unhandled rejection – Gayathri Feb 16 '18 at 11:25
  • In the last example you catch and rethrow. I guess what is calling `Utils.isMaximumLimitReached(data.coachingClassId).catch(()=>{...` isn't catching anything. – HMR Feb 16 '18 at 11:50
  • @HMR Sorry. I corrected it now. EVen this returns unhandled rejection not allowed. – Gayathri Feb 16 '18 at 12:18
  • Your second code sinippet doesn't seem to return the rejected promise at the `then` stage. – Redu Feb 16 '18 at 12:23
  • @Redu second was unorderly. Please consider the present two . edited. – Gayathri Feb 16 '18 at 12:27
  • The last code snippet still doesn't have a `reject` handler, you handle your error in the `resolve` handler. The syntax for `then` is like both answers given is like, with **two** functions as arguments. alternatively, you can change `.then` into `.catch`, which only needs a rejection handler. – Kevin Drost Feb 16 '18 at 12:50
  • @KevinDrost I tried catch of reject too. It gives the same unhandled rejection – Gayathri Feb 16 '18 at 12:58

2 Answers2

1

Promise rejections are usually handled with a second callback in the then method, which could look like this in your case 1:

Utils.isMaximumLimitReached(id).then(()=>{
  // everything is fine
}, (error) => {
  // promise was rejected, handle error
  console.log(error);
})

This way, any rejections caused by isMaximumLimitReached will be handled in the error callback.

S.B.
  • 2,920
  • 1
  • 17
  • 25
1

Change your Promise.reject with

Promise.reject(new Error('409',msg)).then(function() {
  // not called
}, function(error) {
  console.log(error); // Stacktrace
});

Hope this helps and resolve your issue. And If you want to throw the error then you check this before deciding what to use.

Rohan Kadu
  • 1,311
  • 2
  • 12
  • 22