Skip to content

Instantly share code, notes, and snippets.

@queckezz
Last active December 21, 2015 06:19
Show Gist options
  • Save queckezz/6263175 to your computer and use it in GitHub Desktop.
Save queckezz/6263175 to your computer and use it in GitHub Desktop.
Dead-simple Generator implementation preventing callback hell.
var fs = require('fs');
function run ( fn ) {
var generator = fn()
function next( err, data ) {
var ret = generator.next( data )
if ( ret.done ) return;
ret.value( next )
}
next()
}
run(function *(){
var a = yield read('a.txt')
var b = yield read('b.txt')
console.log( a )
console.log( b )
})
function read( file ) {
return function( cb ) {
fs.readFile( file, 'utf8', cb )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment