Once you have wrapped your head around promises checkout async await.
It helps you to write code that is much more readable. When not used properly it has its downsides. You can read Understanding async-await in Javascript to checkout my learnings. If you do not have time to read the full article checkout my thumb rules.
Thumb Rules for async-await
Here are a list of thumb rules I use to keep my head sane around using async
and await
async
functions returns a promise.async
functions use an implicit Promise to return its result. Even if you don’t return a promise explicitlyasync
function makes sure that your code is passed through a promise.await
blocks the code execution within theasync
function, of which it(await statement
) is a part.- There can be multiple
await
statements within a singleasync
function. - When using
async await
make sure to usetry catch
for error handling. - Be extra careful when using
await
within loops and iterators. You might fall into the trap of writing sequentially executing code when it could have been easily done in parallel. await
is always for a single promise.- Promise creation starts the execution of asynchronous functionality.
await
only blocks the code execution within theasync
function. It only makes sure that next line is executed when thepromise
resolves. So if an asynchronous activity has already started thenawait
will not have an effect on it.