The gist of using generators in Node.js

Generators has been there in all languages for quite some time now - it began as a memory efficient alternative to iteration. Lately in the world of javascript, generators have risen above the rank of just another iterative tool and taken the Node.js community by storm. Entire frameworks have been written up from scratch to incorporate generator love.

Does your node servers magically scale by using generators?, insane performance boost maybe? No? So why all this love for generators?

The simple answer is; Generators are your ride home from callback hell. A Code that once looked like this:

fs.readFile('tuts.txt', function(err, fileBuff){
rest_call(fileBuff.toString(), function(err, resp){
db_persist(resp, function(err, status){
// Welcome to the "pyramid of doom" @ Callback hell.
});
});
});

gets transformed into:

run(function*(){
try{
});