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

118
Views
Javascript - Best Practices Using Multiple try/catch within Asynchronous Functions

I've got two files containing asynchronous calls, wrapped in try/catches:

index.js

const fetchData = async () => {
    try {
        const data = await Service.getData()
    } catch (e) {
        // do some error handling
    }
}

Service.js

const Service = {
    getData: async () => {
        try {
            const data = await callApi()
        } catch (e) {
            // do some more error handling
        }
     }
 }

Is the try/catch necessary within fetchData(), since there's already a try/catch in getData()? i.e., is it enough to refactor like so...

const fetchData = async () => {
    const data = await Service.getData()
}

...while letting the service method do all the error handling?

I know this may be a simple question of how to scaffold error handling and personal preference, but I'm mostly looking for what others would implement to reduce boilerplate code

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

0

try cache block is only required in the Service as it is calling API which may return the error sometimes.

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!