Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
why it didn't catch the rejected promise?

I was wondering why the try-catch block couldn't catch the promise rejected from foo.

Frustrated, need help. Thanks in advance.

function addTask(task) {
  setTimeout(() => {
    task.resolver()
  }, 1000)
}

function foo() {
  return new Promise((resolve, reject) => {
    const task = {
      resolver: function() {
        reject('task failed')
      }
    }
    addTask(task)
  })
}

function test() {
  try {
    foo()
  } catch (e) {
    console.error(e)
  }
}

// Uncaught (in promise) task failed
test()
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

It's the same reason that if you had:

const result = foo();

Then you would have a promise stored in result and not the settled value of the promise.

The asynchronous code is started, then the calling function continues.

Sometime later the promise is rejected, but by then it is too late for the code in test() to catch it.


If you had awaited foo(), which would require that test() be async, then it would be caught.

Otherwise you could need to use foo().catch(...).

about 4 years ago · Santiago Gelvez Report

0

Promises are asynchronous but you are not awaiting. It means code continues. So you have to await or you have to use then().catch(). Something like this:

function addTask(task) {
  setTimeout(() => {
    task.resolver()
  }, 1000)
}

function foo() {
  return new Promise((resolve, reject) => {
    const task = {
      resolver: function() {
        reject('task failed')
      }
    }
    addTask(task)
  })
}

function test() {
  foo().then(console.log).catch(console.error);
}

// Uncaught (in promise) task failed
test()

about 4 years ago · Santiago Gelvez Report

0

To simplify your example:

function foo() {
  return new Promise((resolve, reject) => setTimeout(reject, 1000))
}

function test() {
  try {
    foo()
  } catch (e) {
    console.error("Error");
    return;
  }
  console.log("OK!");
}

test()

will print "OK!" indeed have Node.js raise an UnhandledPromiseRejection, as that foo() invocation is just "throwing away" the promise result value. The catch() would only catch synchronous exceptions, not promise rejections.

Turning the function into async function test() and awaiting on foo() will correctly output Error and no unhandled rejection.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!