Skip to content

mrm8488/asyncflow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

asyncflow

Greenkeeper badge

Build Status Coverage Status Known Vulnerabilities

Manage IO operations in Node.js using direct style (sync code like) without callbacks. It works with generators under the hood.

Implementation based on the proposed solution in the great book Node.js Design Patterns

How to install:

npm i asyncflow-gen

How to use it:

const asyncFlow = require('asyncflow-gen');

asyncFlow(function* (callback) {

  yield fs.writeFile('./test/testFile', 'Hey there!', callback);
  
  const data = yield fs.readFile('./test/testFile', 'utf8', callback);
            
  console.log('data is: ', data);

  yield fs.unlink('./test/testFile', callback);
            
  console.log('That is all, folks! Where are the callbacks??');
  
});

What we are avoiding:

fs.writeFile('./test/testFile', 'Hey there!', err => {

  if (err) {
    handleErrorFunction(err);
  }
  
  fs.readFile('./test/testFile', 'utf8', (err, data) => {
  
    if (err) {
      handleErrorFunction(err);
    }
    
    console.log('data is: ', data);
    
    fs.unlink('./test/testFile', err => {
    
      if (err) {
        handleErrorFunction(err);
      }
      
      console.log('That is all, folks! Yes, I do not like this callback cascade.');
      
    });
  
  });

});

About

Manage IO operations in Node.js using direct style (sync code like) without callbacks. It works with generators under the hood

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published