Use Promises over Callbacks, and How to Create a New Promise in Javascript

Sherlynn
The Startup
Published in
2 min readAug 18, 2019

--

Promises are widely used instead of callbacks. Promises are a great feature in Javascript that enable you to avoid callback hell, especially when you need to wait on a response from multiple API requests that are asynchronous.

A Promise is a special Javascript object that represents the eventual result of an asynchronous action. A promise is kind of a proxy for a value that we don’t have yet.

A promise object has 2 internal properties:
1. PromiseStatus
2. PromiseValue

A promise can be in 1 of 3 states:
1. Pending
2. Fulfilled — action fulfilled successfully
3. Rejected — action has failed

There is a class in Javascript called Promise. If you want to create a new promise, you would simply need to create a new instance of this class.

const myPromise = new Promise((resolve, reject) => {});console.log(myPromise);

Initially, the promise is in pending state:

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Sherlynn
Sherlynn

No responses yet