An Essential Guide to JavaScript Promise Basic

Basic usage of state, result, promise.then(), promise.catch() and promise.finally()

Balaji Dharma
2 min readSep 6, 2021

JavaScript promises help to create asynchronous operations. It has two parts of producing code and consuming code.

The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value

The basic syntax for the promise object.

All the examples code using Arrow functions.

// producing code
let promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve('done'), 1000);
});
// Consuming Code
promise.then(
result => console.log(result),
error => console.log(error)
);

State

The initial state is pending. The state is fulfilled when the resolve is called or the rejected state for reject is called.

Result

--

--

No responses yet