Honestly, I had never heard of Promises until I started digging into Windows 8 app development, which uses Promises heavily. In fact, in Windows 8 apps, Promises pretty much take the place of the standard callback style of Javascript, and I have to say (in my opinion) it is a huge improvement and really helps you manage the asynchronous aspects of Javascript development. Here is an example from the
MSDN documentation:
Code: Select all
myWebService.get("http://www.example.com")
.then(function(result) {
return myDb.add(result);
})
.then(function() {
console.log('data successfully saved');
}, function(error) {
console.log('an error occurred while saving:');
console.dir(error);
});
You can see maybe a bit of benefit in added clarity, but that is of course up to personal taste. I personally find this a much better structure than the "callbacks of callbacks of callbacks..." style of vanilla asynchronous Javascript. But where Promises really start to show a lot of benefit is when it comes to synchronizing callbacks so that one will not fire until any others it relies on have fired as well. This is what leads to the very messy looking nested callback structure. Here is another example from MSDN that uses the join() function to basically group all of the actions together so the callback (or "promise"/"then function" in the Promise pattern) for the whole process will not fire until all have completed (note webService.get() is a made-up function for the example). What is happening on the line that starts "var promises" is that the Array.prototype.map() function is being used to convert the array of webservice URLs into an array of promises, so it is basically turning them into little, self-contained tasks that also can handle callbacks:
Code: Select all
function loadData() {
var urls = [
'http://www.example.com/',
'http://www.example.org/',
'http://www.example.net'
];
var promises = urls.map(function (url) {
return myWebService.get(url);
});
WinJS.Promise.join(promises).then(function () {
//do the aggregation here.
});
}
There are even more complex things that can be done with Promises. With names like "join", it sounds like the creator of the pattern had a lot of experience with traditional multithreading, and wanted to bring more of the fine-grained control tricks in multithreading to asynchronous Javascript (which I am totally cool with

). One example would be preventing a callback from firing until another callback in a separate callback chain has completed, which is typically quite messy and error-prone, not to mention that the old techniques for dealing with these situations force a connection between separate parts of your code with little visual relation between them.
Here is the Common JS wiki page that discusses Promises, and they are the ones who maintain the pattern and its several different forms (Windows 8 apps with JS use
Promises/A).
And, probably the most important part of this for most people on these forums, here is a link to
Node-Promise, which is a Node.js Promises module. There is also another one called
Futures.
If anyone is really struggling on how to structure their applications in Node.js (which is nearly 100% asynchronous), I highly suggest to check it out. It may really pay off
