Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda