Should I use Promises or Async-Await

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 asyncand await

  1. async functions returns a promise.
  2. async functions use an implicit Promise to return its result. Even if you don’t return a promise explicitly async function makes sure that your code is passed through a promise.
  3. await blocks the code execution within the async function, of which it(await statement) is a part.
  4. There can be multiple await statements within a single async function.
  5. When using async await make sure to use try catch for error handling.
  6. 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.
  7. await is always for a single promise.
  8. Promise creation starts the execution of asynchronous functionality.
  9. await only blocks the code execution within the async function. It only makes sure that next line is executed when the promise resolves. So if an asynchronous activity has already started then await will not have an effect on it.

read original article here