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

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

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 Relatório

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 Relatório

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 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