0 votes
244 views
in Node.js by (4.4k points)

What are Promises in Node.js ?

2 Answers

0 votes
by (4.4k points)

Promises allow to associate handlers to an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise for the value at some point in the future.

Promises in node.js promised to do some work and then had separate callbacks that would be executed for success and failure as well as handling timeouts. Another way to think of promises in node.js was that they were emitters that could emit only two events: success and error.The cool thing about promises is you can combine them into dependency chains (do Promise C only when Promise A and Promise B complete).

The core idea behind promises is that a promise represents the result of an asynchronous operation. A promise is in one of three different states:

  1. pending - The initial state of a promise.
  2. fulfilled - The state of a promise representing a successful operation.
  3. rejected - The state of a promise representing a failed operation. Once a promise is fulfilled or rejected, it is immutable (i.e. it can never change again).

Creating a Promise

var myPromise = new Promise(function(resolve, reject){

   ....

})

0 votes
by (4.4k points)

Promise has the following states:-

  1. Pending - asynchronous operation is not yet completed.
  2. Fulfilled - asynchronous operation is completed successfully.
  3. Rejected - asynchronous operation is terminated with an error.
  4. Settled - asynchronous operation is either fulfilled or rejected.
  5. Callback - function is executed if the promise is executed with value.
  6. Errback - function is executed if the promise is rejected.a

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...