Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!