Promise in real world


What is a promise

A promise represents the eventual result of an asynchronous operation  A promise is an object that is used as a placeholder for the eventual result of a deferred and possibly asynchronous computation. A promise can be created as

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

From above, it is obvious that promise takes a function with two parameters

  • resolve
  • reject

resolve is a function that should be called when the Promise needs to return the value.

reject is a function that should be called when there is an error condition.

Following is a pseudo code of what a typical promise call will look like


var promiseRemoteData = new Promise(function(resolve,reject){
// 1. Perform ASYNC operation
// 2. On completion of remote operation call
// – resolve to notify success (pass any values that is required)
// – reject to notify error condition
}

Leave a comment

Your email address will not be published. Required fields are marked *