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
While this might be true in general cases, I think that generalisation is too broad and doesn’t do justice to either
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.
catchfor all practical purposes.
- Make sure to write both
.thenmethods for all the promises.
- If something needs to be done in both cases use
- We only get one shot at mutating each promise.
- We can add multiple handlers to a single promise.
- The return type of all the methods in the
Promiseobject, regardless of whether they are static methods or prototype methods, is again a
Promise.all, the order of the promises are maintained in the values variable, irrespective of which promise was first resolved.
Once you have wrapped your head around promises, check out
async-await. It helps you to write code that is much more readable. When it is not used properly, it has its downsides.
Thumb Rules for async-await
Here’s a list of thumb rules that I use to stay sane while using
asyncfunctions return a promise.
asyncfunctions use an implicit
Promiseto return results. Even if you don’t return a promise explicitly, the
asyncfunction makes sure that your code is passed through a promise.
awaitblocks the code execution within the
asyncfunction, of which it (
await statement) is a part.
- There can be multiple
awaitstatements within a single
- When using
async await, make sure you use
try catchfor error handling.
- Be extra careful when using
awaitwithin loops and iterators. You might fall into the trap of writing sequentially-executing code when it could have been easily done in parallel.
awaitis always for a single
Promisecreation starts the execution of asynchronous functionality.
awaitonly blocks the code execution within the
asyncfunction. It only makes sure that the next line is executed when the
promiseresolves. So, if an asynchronous activity has already started,
awaitwill not have any effect on it.
Should I Use Promises or async-await
The answer is that we will use both.
Here are the thumb rules that I use to decide when to use
promises and when to use
async functionreturns a
promise. The converse is also true. Every function that returns a
promisecan be considered as
awaitis used for calling an
async functionand waits for it to
awaitblocks the execution of the code within the
asyncfunction in which it is located.
- If the output of
function2is dependent on the output of
function1, I use
- If two functions can be run in parallel, create two different
async functionsand then run them in parallel.
- To run promises in parallel, create an array of promises and then use
- Every time you use
awaitremember that you are writing blocking code. Over time we tend to neglect this.
- Instead of creating huge
async functionswith many
await asyncFunction()in it, it is better to create smaller
async functions. This way, we will be aware of not writing too much blocking code.
- Another advantage of using smaller
async functionsis that you force yourself to think of which async functions can be run in parallel.
- If your code contains blocking code, it is better to make it an
asyncfunction. By doing this, you are making sure that somebody else can use your function asynchronously.
- By making async functions out of blocking code, you are enabling the user (who will call your function) to decide on the level of asynchronicity they want.
I hope this helps you decide when to use
promises and when to use
You can subscribe to my newsletter https://understandingx.substack.com/ I am lazy to write and hence you may not hear from me for a while.