0

I wrote a Javascript code using async, await and used try, catch in each async function.

Suppose if I write 10 async function then I need to write try, catch in all those 10 functions. Now I am getting question in my mind that doesn't it cause performance overhead?

If it causes any performance overhead then what will be the solution for this problem?

Ziaullhaq Savanur
  • 1,848
  • 2
  • 17
  • 20
  • 1
    You would have to show us the SPECIFIC and REAL code (not pseudo code) for us to comment. There is no generic substitute for `try/catch` if your code needs it. And, ALL performance questions like this ultimately need to be resolved by measuring/testing in a specific JS engine or by realizing that you're microptimizing something that you haven't even proven there is a performance issue with (also done with measuring) and not spending any time worry about it now. – jfriend00 Apr 23 '20 at 05:51

1 Answers1

2

You can write single try catch, in try you can add all of the async-await functions.

try { 
   // service as an example
   const get = await service.get()
   const post = await service.post()
   const delete = await service.delete()
} catch(e) {
  throw new Error(e)
}
onuriltan
  • 3,730
  • 4
  • 25
  • 37