Skip to content

radenkovic/flow-hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flow Hooks

npm GitHub Workflow Status (event) codecov david

Create chainable, reusable hooks to control the flow.

Installation

  • yarn add flow-hooks

Example

Here's a quick example on flow control utilizing all basic hooks.

const { flow, when, cancel, any, every } = require('flow-hooks');

// in some function
const result = await flow([
  (ctx) => {
    ctx.data = [];
  },
  (ctx) => {
    ctx.data.push('first value');
  },
  (ctx) => {
    ctx.run_when = true;
  },
  when(
    (ctx) => !!ctx.run_when, // when to run the hook
    (ctx) => {
      ctx.conditional_run = 'done';
    }
  ),
  (ctx) => {
    ctx.after_when = 'ok';
  },
  any([
    // will pickup the first api call that finished
    someApiCall,
    otherApiCall,
    thirdApiCall,
  ]),
  every([
    // will run all 3 api calls in parallel
    someApiCall,
    otherApiCall,
    thirdApiCall,
  ]),
  cancel(), // stop the flow, do not throw error
  (ctx) => {
    // this will not run because of cancel()
    ctx.after_cancel = 'ok';
  },
]);

Creating a release

git commit --allow-empty -m "Release 1.0.1"