JavaScript Promise
Nothing weights lighter than a promiseThis maybe true regarding to human promises, however in the programming domain, promises are always kept. Following this optimistic note, today we'll be talking about JavaScript promises.
Event Handling Problem
Let's see what promises are good for and their basic capabilities starting with a problem they come to solve. Events are great for things of a repetitive nature like keydown, mousemove etc. With those events you don't really care about what have happened before you attached the listener. On contrary calling services and processing their response is a completely different kind of beast. Have a look at the following function, which reads a json file and returns it's content or an error in case of something goes wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 | function readJSON(filename, callback) { fs.readFile(filename, 'utf8' , function (err, res) { if (err) { return callback(err); } try { res = JSON.parse(res); } catch (ex) { return callback(ex); } callback( null , res); }); } |
What is Promise?
A Promise is an object that is used as a placeholder for the eventual results of a deferred (and possibly asynchronous) computation. A promise can always be situated in one of three different states:
- pending - The initial state of a promise.
- fulfilled - The state of a promise representing a successful operation.
- rejected - The state of a promise representing a failed operation.
- Become fulfilled by a value
- Become rejected with an exception
Cross Platform Support
Over the years developer community has sprung numerous implementations of Promises. The most notable are Q, When, WinJS and RSVP.js, however since our blog focuses on the latest developments in the JavaScript world, we'll be only covering newest Promise class introduced in EcmaScript 6. You can see the browsers' support for the feature here, and in case you wish of your program to work in other browsers, as usually you can use the polyfill.
EcmaScript 6 Promise
The Promise interface represents a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers to an asynchronous action's eventual success or failure. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise of having a value at some point in the future. So let's see our previous example using promises.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function readJSONPromise(filename) { return new Promise( function (resolve, reject) { fs.readFile(filename, 'utf8' , function (err, res) { if (err) { reject(err); } else { try { res = JSON.parse(res); } catch (ex) { reject(ex); return ; } resolve(res); } }); }); } |
1 2 3 4 5 6 7 | readJSONPromise( './example.json' ).then( function onReadFile(res) { return res; }).then( function onProcessFile(response) { console.log( 'response: ' + JSON.stringify(response)); }). catch ( function onError(error) { console.error( 'error: ' + error); }); |
Comments
Post a Comment