Async Await try-catch hell ??????

Async Await try-catch hell ??????

There are many new features in JavaScript, which were helpful for the developer in day-to-day programming. One of them is Async/Await for handling asynchronous function and a cleaner way for handling promises.

No alt text provided for this image

Using Async/Await feels like heaven because it avoids the callback hell by writing asynchronous code in a clean line-by-line format.

No alt text provided for this image

But during error handling for Async/Await, we can end up with try-catch blocks, so all your beautiful one-liners expand to many lines of code??.

No alt text provided for this image

One of the easy ways to resolve this is to append the catch method to the end of each promise. It is better but still gets repetitive.

No alt text provided for this image

Another option is to standardize error handling, which means create a function that implements one try-catch block to replace the others. It first resolves the promise then returns an array where the first element is the data and the second element is the error, but if there is an error, then the data is null and, the error is defined.

No alt text provided for this image

When you call this function in your code, you can destructure it to get a clean one-liner with error handling.

No alt text provided for this image

If you have any other thoughts about the Async/Await or if you know any other techniques for handling promises and asynchronous functions, please put them in the comment box ??????

If you like the content, Please share this article with your friends.

Thanks for reading! Be safe at home!

Happy Coding.

要查看或添加评论,请登录

Prasath Ravichandran的更多文章

  • display: flow-root, A nice little alternative for clearfix in CSS.

    display: flow-root, A nice little alternative for clearfix in CSS.

    There are numerous display properties in CSS, but have you heard of flow-root??? Here are the details, In CSS…

  • React native Reanimated - Quick Look??????

    React native Reanimated - Quick Look??????

    React Native, a popular JavaScript-based UI software framework that allows you to build natively rendering mobile apps…

    2 条评论
  • Scoped storage in Android 11 - React native ??????

    Scoped storage in Android 11 - React native ??????

    From November 2021, the google play console forced the developers to deploy the Android app, targeting API 30(Android…

    3 条评论
  • JS [De,structur,ing] assignment ????

    JS [De,structur,ing] assignment ????

    The destructuring assignment is a javascript expression that takes values from arrays or properties from objects and…

  • What is SvelteJS? How fast is SvelteJS???????

    What is SvelteJS? How fast is SvelteJS???????

    Svelte is a Javascript tool for creating UI components just like React, Angular, and Vue. But Svelte is different from…

  • CSS is too hard(myth) ???

    CSS is too hard(myth) ???

    CSS stands for Cascading style sheet, and it was awesome, ever since it came out in the year 1996 when Netscape was the…

  • An VS Code extension that would replace Postman!!!

    An VS Code extension that would replace Postman!!!

    I have been working in a Postman for testing HTTP requests(REST APIs), but I just found one VS Code extension that…

社区洞察

其他会员也浏览了