Reputation: 61
I have just come along a curiosity of promises. If I reject directly in an chain, I can catch on the variable later.
If I reject on the variable, I can not catch at all. The promise is always considered to be resolved in this case:
let proMISS = Promise.resolve();
proMISS.then(() => console.log('THEN 1'))
.then(() => Promise.reject())
.then(() => console.log('THEN 2'));
setTimeout(() => {
proMISS.catch(() => console.log('CATCH'));
}, 1000);
This indeed does work:
let PROmiss = Promise.resolve()
.then(() => console.log('THEN 1'))
.then(() => Promise.reject())
.then(() => console.log('THEN 2'));
setTimeout(() => {
PROmiss.catch(() => console.log('CATCH'));
}, 1000);
This does not seem to be deterministically
Upvotes: 0
Views: 193
Reputation: 198
let proMISS = Promise.resolve();
let promissTwo = proMISS.then(() => console.log('THEN 1'))
.then(() => Promise.reject())
.then(() => console.log('THEN 2'));
setTimeout(() => {
promissTwo.catch(() => console.log('CATCH'));
}, 1000);
proMISS
chain's output in a new varible promissTwo
and then handled it
Your second example works based on the same concept.
Upvotes: 0
Reputation: 664538
You're essentially doing
let promise1 = Promise.resolve();
let promise2 = promise1.then(() => console.log('THEN 1'))
.then(() => Promise.reject())
.then(() => console.log('THEN 2'));
setTimeout(() => {
promise1.catch(() => console.log('CATCH'));
promise2.catch(() => console.log('CATCH'));
}, 1000);
Yes, promise1
is different from promise2
. The first one is fulfilled with undefined
, while the second is getting rejected.
Upvotes: 1
Reputation: 61
I just found the answer, I guess.
The promise in the variable is resolved, but the chain is not. Therefore, if you catch on the variable, it has to be resolved.
You would have to save the last member of the chain, every-time you add a member.
Upvotes: 1