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:

		var fileBuff = yield readFile('tuts.txt');
        var resp = yield rest_call(fileBuff);
        var status = yield db_persist(resp);
    } catch(err) {

Pretty neat, huh? Of course there are some nuts and bolts that need to be attached elsewhere, but the general idea is that asynchronous code can be written in a pseudo-synchronous linear fashion.

There are great tutorials that explain how to get about using generators during node development, pointing to some for reference: