Last active
          December 21, 2015 06:19 
        
      - 
      
- 
        Save queckezz/6263175 to your computer and use it in GitHub Desktop. 
    Dead-simple Generator implementation preventing callback hell.
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | file:a | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | file:b | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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