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

177
Views
Why can't function A catch the error thrown by function B

I'm doing try and catch exercises in JavaScript.

here is my code

function getTime1() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(11111);
    }, 1000);
  });
}

function getTime2() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(22222);
    }, 1000);
  });
}

async function funB() {
  // try {
  let b = await getTime2();
  throw "B error";
  // } catch (e) {
  //   console.log("this" + e);
  // }
}

async function funA() {
  try {
    let a = await getTime1();
    funB();
  } catch (e) {
    console.log("that" + e);
  }
}
funA();

and here is the result

Uncaught (in promise) B error

I want to know why there is no catch in funb and Funa. Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From my understanding try/catch doesn't work with async :

"Because errorTest is async, it will always return a promise and will never begin execution exactly where you call it: it is asynchronous. errorTest returns, and you exit the try block, before a single line of code within errorTest is run. Therefore, your catch block will never fire, because nothing in errorTest would synchronously throw an exception."

Source : Why is try {} .. catch() not working with async/await function?

Since async will return a Promise, I believe you'll need to use the catch on the Promise itself

funB().then((result) => {
    console.log(result);
}).catch((error) => {
    console.error(error);
});
about 4 years ago · Juan Pablo Isaza 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!