NodeJS By Examples

Promises

A Promise is an object that represents the result of an asynchronous function call and it's a valuable tool for managing asynchronous control flow.

 

Content:

1. Intoduction

2. Callback examples and callback hell

3. Promises and promises/A+

4. Promise terminology: fullfilment, rejection, resolution, callback, errorback, progressback

5. Promise NPM libraries for NodeJS: q, bluebird

  5.1 q promise

  5.2 bluebird promise

6. Chaining and nesting promises

7. Error handling in promises

 

 

Promise terminology:

  • Fulfillment: When a successful promise is fulfilled, all of the pending callbacks are called with the value. If more callbacks are registered in the future, they will be called with the same value. Fulfilment is the asynchronous analog for returning a value.
  • Rejection: When a promise cannot be fulfilled, a promise is 'rejected' which invokes the errbacks that are waiting and remembers the error that was rejected for future errbacks that are attached. Rejection is the asynchronous analog for throwing an exception.
  • Resolution: A promise is resolved when it makes progress toward fulfillment or rejection. A promise can only be resolved once, and it can be resolved with a promise instead of a fulfillment or rejection.
  • Callback: A function executed if a a promise is fulfilled with a value.
  • Errback: A function executed if a promise is rejected, with an exception.
  • Progressback: A function executed to show that progress has been made toward resolution of a promise.