DEV Community

ahsanmalik07
ahsanmalik07

Posted on

Promising your asynchronous operation

Incase of multiple async operations, the use of callback can quickly result in CALLBACK HELL. Callback hell is when visually, execution is happening top to bottom.

Alt Text

With the introduction of promise the chaining of functions was made simple and straightforward, simplified the code and improved its readability.

Promises in JavaScript
Promise is an JavaScript object that comprises of 3 states:

Pending: Initial State, before the Promise succeeds or fails
Resolved: Completed Promise
Rejected: Failed Promise

Alt Text

Creating a promise is simple matter of calling the promise constructor. The resolve and reject functions for settling the promise state, are passed to the promise constructor:

Alt Text

A simple subscription can be made to the fate( settled state of promise) by using .then (if resolved) or .catch (if rejected).

Alt Text

Alt Text

WRAPPING UP
Promise is a broader topic that cannot be summed up in one article. The aim of this post was to get you familiarized with Promises in JavaScript and how to create and use them

Top comments (0)