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

204
Vistas
Catch a JavaScript error from a upper function

I'm stuck with the following problem:

function upperFn(){
    FetchSomeThing() 
    .catch(err => { 
        console.log(err) 
    })
}

function lowerFn(){
    try {
        upperFn()
    }
    catch (err){
        //Here its not working anymore
        console.log(err) // Catch is never used
    }
}

So I've tried to return the error and even rethrow it but nothing work's. I would be very happy if someone can explain me how to catch this error in my lower function.

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I just figure out how to handle this probleme, simply by returning the whole function.

function upperFn(){
    return FetchSomeThing()
}

function lowerFn(){
    try {
        upperFn()
    }
    catch (err){
        //Here its working well
        console.log(err)
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Simply throw the error in upperFunction and try-catch it in the lowerFunction

function FetchSomeThing() {
  return Promise.reject("Something goes wrong.");
}

function upperFn(){
  FetchSomeThing().catch(error => {
    throw error;
  })
}

function lowerFn(){
  try {
      upperFn()
  }
  catch (error) {
      //Here its not working anymore
      console.log(error) // Catch is never used
  }
}

lowerFn()

Working example: https://codesandbox.io/s/agitated-thunder-3s9qj

Output:

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe it's because you missed the error param after catch?

try {
  doSomeThing();
} catch (error) {
  console.error(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