What is Promise in JavaScript?

If you’re new to JavaScript, you might be wondering what a promise is. In short, a promise is an object that represents the result of an asynchronous operation. In this blog post, we’ll take a closer look at promises and how they can be used in your code.

Checkout this video:

Introduction

A promise is an object that may produce a single value some time in the future: either a resolved value, or a reason that it’s not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states:

-fulfilled: The action relating to the promise succeeded
-rejected: The action relating to the promise failed
-pending: Hasn’t fulfilled or rejected yet

What is promise?

In JavaScript, a promise is an object that represents the result of an asynchronous operation. A promise can be in one of three states: pending, fulfilled, or rejected.

Promises are used to handle asynchronous operations in JavaScript. When you make a request using a promise, you start a “chain” of promises. The first promise in the chain is called the “root” promise, and the last promise in the chain is called the “leaf” promise. Each promise in the chain either resolves or rejects, and each subsequent promise is resolved or rejected based on the previous promise’s status.

A pendingpromiseis a Promise that hasn’t been resolved or rejected yet. A fulfilledpromiseis a Promise that has been resolved. A rejectedpromiseis a Promise that has been rejected.

JavaScript Promises are similar to synchronous programming’s try/catch/finally blocks but with some important differences:
– In synchronous programming, if an exception is thrown inside the try block, execution will jump to the finally block immediately (if it exists) and then continue after the finally block.Promises work differently – if an error is thrown inside a function that is chained off of a Promise, it will be caught by the next ‘catch’ handler down the line instead of jumping to the finally block.”,

– In synchronous programming, you can decide whether or not to catch an exception inside a catch block (by using a ‘throw’ statement). With Promises, there is no way to “catch” an error inside a catch handler – errors always propagate down the chain.”,

– In synchronous programming, you can nest try/catch blocks inside each other. With Promises, catch handlers are always executed after then handlers.”

Why we use promise?

Every once in a awhile, you may find yourself writing code that looks something like this:

“`JavaScript
doThing1(function (value1) {
doThing2(value1, function(value2) {
doThing3(value2, function(value3) {
console.log(‘got the value: ‘ + value3);
});
});
});
“`

How promise works?

A promise in JavaScript is an object that represents the result of an asynchronous operation. A promise can be in one of three states:

pending: The initial state of a promise, meaning the async operation hasn’t completed yet.
resolved: The async operation has completed successfully.
rejected: The async operation has failed.

A promise is considered settled if it’s not pending anymore, meaning it’s either resolved or rejected.
You can think of a pendingpromise as a placeholder for a future value. That future value could be resolved (if everything goes as planned), or rejected (if something goes wrong).

Once a promise is settled, it can’t go back to being pending — the promise is considered immutable (unchanging) at that point.

Promise API

A promise is an object that represents the result of an asynchronous operation. A promise can be in one of three states:

pending – The initial state of a promise, represents that the asynchronous operation has not yet completed.
fulfilled – The state of a promise representing that the asynchronous operation has completed successfully.
rejected – The state of a promise representing that the asynchronous operation has failed.

A pending promise can transition to either a fulfilled or rejected state. A fulfilled or rejected promise is considered settled, meaning it will never transition to any other state.

Promise libraries

There are many promise libraries available for JavaScript, but they all essentially provide the same functionality. The most popular promise libraries are:

-Bluebird
-RSVP
– when
– Q
– deferred

Conclusion

When using a promise, you can think of it as a “promise” to return a value at some future point in time. In other words, it is an agreed upon contract between you and the code that is executing. The promise object has two functions, .then() and .catch(), that determine what happens when the promise resolves or reject. A resolved promise means that the async operation has completed successfully and .then() will be executed. A rejected promise indicates that the async operation was not successful and .catch() will be executed.

Scroll to Top