promise
Blogs20132013-12-12
From what I have understood there are three ways of calling asynchronous code:
- Events: eg. request.on(“event”), callback);
- Callbacks: eg. fs.open(path, flags, mode, callback);
- Promises
Promises in node.js promised to do some work and then had separate callbacks that would be executed for success and failure as well as handling timeouts. Another way to think of promises in node.js was that they were emitters that could emit only two events: success and error.
A Promise is a programming model that deals with deferred results in concurrent programming. The basic idea around promises are that rather than issuing a blocking call for a resource (IO, Network, etc.) you can immediately return a promise for a future value that will eventually be fulfilled. This allows you to write non-blocking logic that executes asynchronously without having to write a lot of synchronization and plumbing code.
More information: Promises/A jQuery Deferred q: Defer/when style promise API for JavaScript
I use cujojs/when.js, and angularjs is Q library.
