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

186
Views
Handle errors inside promise scope funcions

I have an async function which return promise In this promise I have scope function and inside that I have 2 other scope functions If I try to throw error on function a the code continues it`s execution

    async someAsyncFunc(): Promise<void> {
      return new Promise((resolve, reject) => {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let port: any;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        let writer: any;
        const test = async () => {
          const a = async () => {
            throw new Error();
          };

          const b = async () => {
            // do some work
            await a();
          };

          try {
            await b();
          } catch (err) {
            console.log(err);
          }

          resolve();
        };

        try {
          test().catch((err) => {
            console.log("catch block error");
            console.error(err);
            reject(err);
          });
        } catch (error) {
          return reject(error);
        } finally {
        }
      });
    }

I want somehow to handle all errors that could happen on either functions a,b and load at this final try catch block of the returned promise

try {
    test().catch((err) => {
        console.log("catch block error");
        console.error(err);
        reject(err);
          });
        } catch (error) {
          return reject(error);
        } finally {
        }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

      try {
        await b();
      } catch (err) {
        console.log(err);
      }

You're catching and handling the error there.

You can't get it in test()'s catch handler because no error is raised to that point.

It's already been handled.

You'd need to throw it again if you want something else to catch it:

  try {
    await b();
  } catch (err) {
    console.log(err);
    throw err;
  }
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!