Socket
Book a DemoInstallSign in
Socket

gensync-cb

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gensync-cb

Small module for writing async callback code in a more syncrinous way using generator functions.

1.0.5
latest
npmnpm
Version published
Maintainers
1
Created
Source

gensync-cb

Small module for writing async callback code in a more syncrinous way using generator functions.

useage example:

  let gensync = require('gensync-cb'),
      fs = require('fs');
  
  function processFile(filePath, callback){
    
    gensync.syncronise(function*(resume){
    
      var [exists] = yield fs.exists(filePath, resume);
      
      if(!exists) return callback('file not found');
        
      var [error, fileContent] = yield fs.readFile(filePath, 'utf8', resume);
      
      if(error) return callback(error);
      
      //do some processing
      
      var newFileContent = '';
      
      var [error] = yield fs.writeFile(filePath, newFileContent, resume);
      
      if(error) return callback(error);

      callback(null, `processed file ${filePath}`);
   
    });
  }
  
  processFile('file.txt', console.log);

Also includes a helper function for running multiple async functions in parallel, with the option to limit the number of functions running simultaniously.


let funcs = [cb=>processFile('file.txt', cb), cb=>processFile('file2.txt', cb)];

gensync.parallel(5, false, funcs, () => console.log('done processing files'));

//same as above, curried
gensync.parallelCurry(5)(false)(funcs)(() => console.log('done processing files'));

FAQs

Package last updated on 24 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.