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

183
Vistas
Is there any way to await and catch in javascript?

I am kind of new to async/await in javascript by have used suspend functionalities in Kotlin before. Is there any way to await and catch error if any in js. (without writing try catch). If not what is the right way to do this.

For example consider this snippet, async function a:

let a = async () => {
   // does io and returns result
}

Now, consider a function b which calls a. Can I write it in this clean way:

let a = async () => {
   const result_of_a = (await a()).catch(() => { // handle error }) 
   // work with `result_of_a`
}

Rather than

let a = async () => {
   var result_of_a = null
   try {
       result_of_a = await a()
   } catch (e) {

   }
   // work with `result_of_a`
}

Is there any other pretty way of writing the above sub-routine

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

0

I don't believe you can. If you think .catch is cleaner perhaps you should just use promise syntax. Personally I use both, depending on the context. But I favor async due to promise hell

let asyncFn = async () => {
   const result = a().then(...).catch(...) 
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Can you try this way :

   let b = async () => {
       const result_of_a = await a().then(()=>{
         // handle success response here and return value
        },
        ()=>{
           // handle success response here and return value you want
          }
       )

   // You will get value return in `result_of_a`
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Use catch on a promise that is returned from your function. You mixed up two different approaches.

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