I was experimenting with this code:
let with999 = Promise.resolve(999);
let returnCatch = with999
.catch(reason => {
console.log("catch: " + reason);
});
returnCatch.then(data => {
console.log("then: " + data);
});
when I suddenly realized that:
The promise with999
is fulfilled and therefore the catch()
method is not executed, however, the promise returned by catch()
(returnCatch
in this case) ends up fulfilled with the same value as with999
.
So, my question is, why the catch()
ends up fulfilled the returnCatch
promise?
I expected returnCatch
to be pending (because catch()
was not executed) and consuming it with then()
nothing will happen.
The same happens "doing the opposite", then()
rejects a promise:
let rejected = Promise.reject(new Error('Ups!'));
let returnThen = rejected
.then(reason => {
console.log("then: " + reason);
});
returnThen.
catch(data => {
console.log("catch: " + data);
});
Can someone explain to me what is going on?