Promise chain continues after rejection

I’m having trouble to properly catch an error/reject in a promise chain.

const p1 = () => {
    return new Promise((resolve, reject) => {
        console.log("P1");
        resolve();
    });
};

const p2 = () => {
    return new Promise((resolve, reject) => {
        console.log("P2");
        reject();
    });
};


const p3 = () => {
    return new Promise((resolve, reject) => {
        console.log("P3");
        resolve();
    });
};

p1().catch(() => {
    console.log("Caught p1");
}).then(p2).catch(() => {
    console.log("Caught p2");
}).then(p3).catch(() => {
    console.log("Caught p3");
}).then(() => {
    console.log("Final then");
});

When the promise is rejected, the following .then still gets executed. In my understanding, when in a promise chain an error/reject happened, the .then calls that follow it are not executed any more.

P1
P2
Caught p2
P3
Final then

The rejection gets caught correctly, but why is "P3" logged after the catch?

What am I doing wrong?

To clarify @evolutionxbox, this is my expected result:

Promise.resolve().then(() => {
    console.log("resolve #1");
    return Promise.reject();
}).then(() => {
    console.log("resolve #2");
    return Promise.resolve();
}).then(() => {
    console.log("resolve #3");
    return Promise.resolve();
}).then(() => {
    console.log("Final end");
}).catch(() => {
    console.log("Caught");
});

This code works exactly like it should. And I can’t see a difference to my code, except that I declared the functions separately.

The code above stops no matter where the promise is rejected.

152 thoughts on “Promise chain continues after rejection”

  1. Thanks , I have recently been looking for info approximately this topic for ages and yours is the greatest I’ve found out till now. But, what about the conclusion? Are you sure in regards to the source?

    Reply
  2. hi!,I like your writing very so much! proportion we communicate more about your article on AOL? I require a specialist in this area to solve my problem. May be that is you! Looking ahead to peer you.

    Reply
  3. I am curious to find out what blog system you happen to be working with? I’m having some small security issues with my latest blog and I’d like to find something more risk-free. Do you have any recommendations?

    Reply

Leave a Comment