What is Promise in javascript?

Hi, my question is :
What is Promise in javascript ? and what does it do exactly ?
I want to know what is happening under the hood when we use a promise in our code & what is the benefit of using promises ?
Thank you in advance :slightly_smiling_face: :+1:

2 Likes

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, rejected, or pending. Promise users can attach callbacks to handle the fulfilled value or the reason for rejection

3 Likes

Thank you, appreciated :ok_hand:

1 Like

Not need it to thank meπŸ˜€

Hi,
Promise is designed to resolve callback hell and reverse controll. In front-end develop, the promise is not used quite often. It always packaged already in some library (like, axios). Unless you are a backend developer using node or an Animation web developer. Learning the concept may be more important.

Check this https://www.youtube.com/watch?v=KpGmW_P5Ygg
I think It demostrated the mechanism of asynchronization really well.

1 Like

I will always support you! It really means a lot to me word unscrambler

A promise is exactly like its plain meaning. You get a promise from your PM that he will do something in the future. You have received it already. The promise acts as a container of his future deliverable result. Your PM might deliver the result, forfeit it, or still in the process of getting it. You will have to wait some time to know. But for now, you have your PM’s promise or a word given to you.

Hello @A100D-JS

This question might be old, but allow me to explain.

A promise is just another way to get the result of an asynchronous operation sometime in the future. It is a simple object based on the callback approach, but created to replace callbacks because of the callback hell problem.

Throughout its life a promise may be of three states:

  • Pending : The operation is yet to return a response.
  • Resolved : The operation returned a result and was successful.
  • Rejected : The operation failed and did not return a result.

Hope this helps :grinning:

1 Like

A promise in JavaScript is similar to a promise in real life. When we make a promise in real life, it is a guarantee that we are going to do something in the future. Because promises can only be made for the future. A promise has 2 possible outcomes: it will either be kept when the time comes, or it won’t. So, when we define a promise in JavaScript, it will be resolved when the time comes, or it will get rejected.

There are 3 states of the Promise object:

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

Visit those helpful websites :point_down:

  1. Javascript Promise

  2. Javascript Promise