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
I found a promise library https://github.com/kriszyp/node-promise but I don't get it.
Could someone explain what promises are all about and why I should use it?
Also, why was it removed from Node.js?
Another advantage of promises is that error handling and exception throwing and catching is much better than trying to handle that with callbacks.
The bluebird library implements promises and gives you great long stack traces, is very fast, and warns about uncaught errors. It also is faster and uses less memory than the other promise libraries, according to http://bluebirdjs.com/docs/benchmarks.html
This new tutorial on Promises from the author of PouchDB is probably the best I've seen anywhere. It wisely covers the classic rookie mistakes showing you correct usage patterns and even a few anti-patterns that are still commonly used - even in other tutorials!!
Enjoy!
PS I didn't answer some other parts of this question as they've been well covered by others.