Reputation: 1896
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;
})
Upvotes: 1
Views: 1683
Reputation: 1389
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.
Upvotes: 1
Reputation: 2969
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.
Upvotes: 1