2

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

1
  • Its enough to have 1 try catch block per function. If you want to return different messages you can throw . Commented Mar 16, 2022 at 12:06

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.