Once you have wrapped your head around promises checkout
Thumb Rules for async-await
Here are a list of thumb rules I use to keep my head sane around using
asyncfunctions returns a promise.
asyncfunctions use an implicit Promise to return its result. Even if you don’t return a promise explicitly
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 awaitmake sure to 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 promise.
- Promise creation starts the execution of asynchronous functionality.
awaitonly blocks the code execution within the
asyncfunction. It only makes sure that next line is executed when the
promiseresolves. So if an asynchronous activity has already started then
awaitwill not have an effect on it.