1. Catching Errors Twice

Are we allowed to catch errors two times? Yes.

const P = new Promise((resolve, reject) => reject("There is an error!"));
P.catch((e) => console.log(e));
P.catch((e) => console.log(e));

// Output:
There is an error!
There is an error!

2. Attaching then after catch

Will it work if we add then after catch? If we return in catch, the Yes else No.

new Promise((resolve, reject) => reject("There is an error!"))
  .catch((e) => console.log(e))
  .then((e) => console.log(e));

// Output:
There is an error!
undefined

------------------

new Promise((resolve, reject) => reject("There is an error!"))
  .catch((e) => {
    console.log(e)
    return e
  })
  .then((e) => console.log(e));

// Output:
There is an error!
There is an error!

3. Throwing error in then

Promise Resolved -> Moves to then -> Throws Error -> Catched in catch -> returns message -> logged using then

new Promise((resolve, reject) => {
  resolve("Success!");
})
  .then(() => {
    throw Error("Oh noes!");
  })
  .catch((error) => {
    return "actually, that worked";
  })
  .then((message) => console.log(message));

// Output:
actually, that worked

4. Return and modify in then

Promise.resolve("Success!")
  .then((data) => {
    return data.toUpperCase();
  })
  .then((data) => {
    console.log(data);
  });

// Output:
SUCCESS!

5. Return in catch followed by Error in then

Promise.resolve("Success!")
  .then(() => {
    throw Error("Oh noes!");
  })
  .catch((error) => {
    return "actually, that worked";
  })
  .then((data) => {
    throw Error("The fails!");
  })
  .catch((error) => console.log(error.message));

// Output:
The fails!

6. args and return in finally

finally donot take any arguments, and cannot return any value. then takes the value from previous then chaining.

const promise = new Promise((res) => res(2));
promise
  .then((v) => {
    console.log(v);
    return v * 2;
  })
  .then((v) => {
    console.log(v);
    return v * 2;
  })
  .finally((v) => {
    console.log(v, "finally");
    return v * 2;
  })
  .then((v) => {
    console.log(v);
  });

// Output:
2
4
undefined finally
8

catch wont get access to v from previous then

const promise = new Promise((res) => res(2));
promise
  .then((v) => {
    console.log(v);
    return v * 2;
  })
  .then((v) => {
    console.log(v);
    throw Error("There is a error!");
  })
  .catch((err) => {
    console.log(err.message);
    console.log(v);
    return v * 2;
  })
  .finally((v) => {
    console.log(v, "finally");
    return v * 2;
  })
  .then((v) => {
    console.log(v);
  });

// Output:
ReferenceError: v is not defined inside the catch

then after finally can take return data from both catch and then

const promise = new Promise((res) => res(2));
promise
  .then((v) => {
    console.log(v);
    return v * 2;
  })
  .then((v) => {
    console.log(v);
    throw Error("There is a error!");
  })
  .catch((err) => {
    console.log(err.message);
    return "8 from Error";
  })
  .finally((v) => {
    console.log(v, "finally");
    return v * 2;
  })
  .then((v) => {
    console.log(v);
  });

// Output:
2
4
There is a error!
undefined finally
8 from Error