-1

Hello I stumbled across an issue which I am not sure if i am just misunderstanding the ways Promises work or not. I am trying to catch unhandledPromiseRejection by wrapping all code inside a method in a parent promise so that it can catch the propagated errors.


function testFunction() {
  new Promise((resolve,reject)=>{

    const innerPromiseWithError = new Promise(resolve=>{
      throw new Error("TESTTEST");
    })

    innerPromiseWithError.then();

    //do smth else here;
  }).catch(console.log);

Here I expect the catch to catch the rejected promise with the error caused by innerPromise but that is not being the case. Is there a flaw in my logic on how I am expecting this to be handled?

  • Relevant: [What is the explicit promise construction antipattern and how do I avoid it?](https://stackoverflow.com/q/23803743) – VLAZ Apr 03 '23 at 20:17

1 Answers1

0

It is the example of promise

function promise() {
  return new Promise(function (resolve, reject) {
        const a = 4
        const b = 5
        const c = 5

        const sum = a + b;
        if (sum <= c) {
              resolve("Let's go!!" + " " + sum)
        } else {
              reject(`Oops!.. Number must be less than : ${c}`)
        }
  })
};
promise().then((ele) => {
     console.log("true", ele)
}).catch((err) => {
     console.log(err)
})