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?
The key thing here — and one of the key things about using promises — is that then
and catch
create new promises. So the promise that was rejected that wasn't handled was the one created by then
:
let promise = request(false);
promise.then( response => { // Creates new promise, rejection isn't handled
console.log('response' , response);
});
promise.catch( (err) => { // Creates new promise
console.log('got Error', err);
});
This is one of the reasons you see promise chains:
request(false)
.then( response => {
console.log('response' , response);
})
.catch( (err) => {
console.log('got Error', err);
});
There, three promises are still created (the original from request
, the one from then
, and the one from catch
), but rejection is handled for all three of them by the final catch
handler.
The promise created by then
and catch
work like this:
then
handler, resolve with the resolution from the original promisethen
handler, call it:
catch
handler, reject with the rejection from the original promisecatch
handler, call it and do exactly what's done with the then
handler above (resolve or reject based on what it returns or throws)promise.then()
creates a new promise whose settlement depends on promise
. When promise
is rejected, the implicit promise created by promise.then()
is also rejected and has no catch()
clause to handle the error.
What you need to do is chain the .catch()
to the promise returned by promise.then()
:
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);
}).catch( (err) => {
console.log('got Error', err);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With