Javascript: Understanding Promise

Promise is a proxy value for an asynchronous action leading upto success or a failure. Using Promise is necessary to avoid the callback hell

A pending promise can either be fulfilled with a value, or rejected with a reason (error). When either of these options happens, the associated handlers queued up by a promise's then method are called.

A Promise can be in one of the following states

Promise.prototype.catch() will handle the errors thrown by the asynchronous flow execution of the Promise where as catch() of try-catch will catch the errors triggered by the synchronous flow

Promise are always asynchronous

You can also resolve function statically like Promise.resolve() - Promise.reject() instead of calling the constructor new Promise((resolve, reject) => {});

Promise are immediately executing

Unlike callbacks, A promise will be executed as soon as it is encountered.

Async & Await

Async & Await is an ECMAScript 2017 standard

An async function is a function which is operated asynchronously via event loop returning an AsyncFunction object. It uses promises to return its result. await is to get the resultant from the promised function.

Remember, the await keyword is only valid inside async functions. If you use it outside of an async function's body, you will get a SyntaxError.

We shouldn't make it sequentially,

For instance,

You can clearly see that, parallel() is twice as fast as series() is because it is not sequential.


For my new posts, Subscribe via weekly RSS, common RSS or Subscribe via Email