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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |