I have following code
function request(status){
return new Promise((resolve, reject) => {
setTimeout(() => {
if(status){
resolve('Success');
} else {
reject('error');
}
}, 1000);
});
}
let promise = request(false);
promise.then( response => {
console.log('response' , response);
});
promise.catch( (err) => {
console.log('got Error', err);
});
throws following error even I caught the reject response
got Error error (node:11252) UnhandledPromiseRejectionWarning: error (node:11252) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:11252) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
but if I remove the then
block then it works fine,
NO STACK TRACE ERROR ON THE CONSOLE
function request(status){
return new Promise((resolve, reject) => {
setTimeout(() => {
if(status){
resolve('Success');
} else {
reject('error');
}
}, 1000);
});
}
let promise = request(false);
promise.catch( (err) => {
console.log('got Error', err);
});
Output:
got Error error
I do not understand why it works in such a way?