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

138
Visualizações
Return from a function when one of try ... catch block throws an error

I've got a function where I submit a large form and it makes multiple API calls. I tried to separate it into smaller functions cause there is some additional logic that depends on API response. I wrapped each API call within a function in try ... catch so that I have more control over the errors. The problem is that I need to terminate the parent function whenever one of the child function throws an error and I can't figure out the clean way of doing that.

So the code is the following:

const func1 = async() => {
    try {
        // api call + logic
    } catch (error) {
        // show error toast and terminate formSubmit function
    }
}

const func2 = async() => {
    try {
        // api call + logic
    } catch (error) {
        // show error toast and terminate formSubmit function
    }
}

const func3 = async() => {
    try {
        // api call + logic
    } catch (error) {
        // show error toast and terminate formSubmit function
    }
}


const formSubmit = async () => {
    await func1()
    await func2()
    await func3()
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Just throw a new error or rethrow the error and then add a new handler in your formSubmit function.

const func1 = async() => {
    try {
        // api call + logic
    } catch (error) {
        // show error toast and terminate formSubmit function
        throw new Error("...");
    }
}

const func2 = async() => {
    try {
        // api call + logic
    } catch (error) {
        // show error toast and terminate formSubmit function
        throw new Error("...");
    }
}

const func3 = async() => {
    try {
        // api call + logic
    } catch (error) {
        // show error toast and terminate formSubmit function
        throw new Error("...");
    }
}


const formSubmit = async () => {
    try {
        await func1()
        await func2()
        await func3()
    } catch(e){
       // do what needs to be done on error
    }

}
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