I have a controller.js from where I call an async method in the service.js . Despite having a '.catch' block to deal with the Promise rejection, the program never goes there. It ALWAYS enters the '.then' block before any Promise is resolved.
Here's the code for the "controller.js" (the caller)
const Service = require('./service')
Service.serviceQueryDB("abc")
.then(result => /* Immediately comes here, not waiting for the Promise to be resolved */
console.log("I should have waited, but I'm already here. Resolved : ", result))
.catch(e => /* Never enters this block. The error thrown by Service.serviceQueryDB is never caught
because the program runs immediately to the '.then' block */
console.error("Rejected : ", e))
Here's the code for the "service.js" (the provider)
async function queryDB(str){
return new Promise((_,rej) =>{
setTimeout(()=>{
console.log("Program is on DB now")
rej("Query done!") /*I am 'rej'ecting the Promise*/
},1000)
})
}
async function serviceQueryDB(str){
setTimeout(()=>{
queryDB(str)
.then( _ => {
return new Promise((res)=>{
res("Query sucessfull")
})
})
.catch(e => { /* the Promise rejection from the queryDB() is caught HERE, which is fine */
return new Promise((_,rej) => {
console.error(e) /* prints this 'Query done!', which is fine */
rej("Query unsuccessful") /* however, this 'rej'ection is NOT caught in the controller,
from where the method is called. Why? */
})
})
}, 1000)
}
module.exports = {queryDB, serviceQueryDB}
So, the exception thrown should be caught on the controller.js and "Query unsuccessful" should be printed in the variable result here: console.log("I should have waited, but I'm already here. Resolved : ", result))
PS - if you copy/paste the code into two separate files and run node .\controller.js, you'll be able to reproduce the error
Thanks in advance