Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

136
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda