Member-only story
JavaScript: Promises or async-await
A set of rules for when to use which
I recently read a medium post where the author claimed that using async-await
is better than using promises
.
While this might be true in general cases, I think that generalisation is too broad and doesn’t do justice to either async-await
or promises
.
For someone new to JavaScript, making sense of these and deciding which one to use can be a challenge. In this post, I will list things that I have learned about these and how I decide when to use which.
I read somewhere that async-await
is syntactical sugar for using promises
. So, before getting to know async-await
or deciding which approach to use, make sure that you have a good understanding of promises and async-await.
Here are some thumb rules that I follow.
Thumb Rules for Using Promises
- Use promises whenever you are using asynchronous or blocking code.
resolve
maps tothen
andreject
maps tocatch
for all practical purposes.- Make sure to write both
.catch
and.then
methods for all…