JavaScript: Concept of Promise

Dear readers, this post is about a very useful and important concept of JavaScript. I think every beginner should have this basic knowledge while developing any project.

What is a Promise

In JavaScript, Promise is a very important concept.T…


This content originally appeared on DEV Community and was authored by Swarnali Roy

Dear readers, this post is about a very useful and important concept of JavaScript. I think every beginner should have this basic knowledge while developing any project.

What is a Promise

In JavaScript, Promise is a very important concept.The Promise object represents the eventual completion or failure of an asynchronous operation and its resulting value.

?? A Promise is a proxy with an unknown value whenever a Promise is generated.
?? This allows the controller to connect with the final success value or reason for failure of the asynchronous operation.
?? This allows asynchronous methods to return the same values ​​as synchronous methods.
??Instead of returning the final value immediately, the asynchronous method returns a promise to provide the value at a particular point in the future.

Basic Syntax

Promise is a constructor function, so we need a new to create a Promise. It takes a function, as it's argument, with two parameters: resolve and reject.
The syntax generally looks like:

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

States of a Promise

A promise has three states:

i) pending: It's the initial state, neither successful nor unsuccessful.

ii) fulfilled: It means that the operation was completed successfully.

iii) rejected: It indicates that the operation has failed.

Resolve Parameter

The resolve parameter is used when we want the promise to succeed.

Reject Parameter

The reject is used when we want to catch the failure.

The following is an example of a Promise:

const makeServerRequest = new Promise((resolve, reject1) => {
  let responseFromServer;

  if (responseFromServer) {
    resolve("We got the Data");
  } else {
    reject("Data not Found");
  }
});

In the code snippet, responseFromServer represents a response from the server.

If responseFromServer is true, the resolve method will be called to successfully complete the promise and return the string as it's argument.

(Generally, it returns data)

Again, if responseFromServer is false, the promise will fail and call the reject method.

(Generally it catches the errors).


This content originally appeared on DEV Community and was authored by Swarnali Roy


Print Share Comment Cite Upload Translate Updates
APA

Swarnali Roy | Sciencx (2021-08-01T20:42:44+00:00) JavaScript: Concept of Promise. Retrieved from https://www.scien.cx/2021/08/01/javascript-concept-of-promise/

MLA
" » JavaScript: Concept of Promise." Swarnali Roy | Sciencx - Sunday August 1, 2021, https://www.scien.cx/2021/08/01/javascript-concept-of-promise/
HARVARD
Swarnali Roy | Sciencx Sunday August 1, 2021 » JavaScript: Concept of Promise., viewed ,<https://www.scien.cx/2021/08/01/javascript-concept-of-promise/>
VANCOUVER
Swarnali Roy | Sciencx - » JavaScript: Concept of Promise. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/01/javascript-concept-of-promise/
CHICAGO
" » JavaScript: Concept of Promise." Swarnali Roy | Sciencx - Accessed . https://www.scien.cx/2021/08/01/javascript-concept-of-promise/
IEEE
" » JavaScript: Concept of Promise." Swarnali Roy | Sciencx [Online]. Available: https://www.scien.cx/2021/08/01/javascript-concept-of-promise/. [Accessed: ]
rf:citation
» JavaScript: Concept of Promise | Swarnali Roy | Sciencx | https://www.scien.cx/2021/08/01/javascript-concept-of-promise/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.