Search code examples
javascriptecmascript-6promisees6-promise

Promise rejection not passed onto catch, if stored in variable


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


Solution

  • 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.