Skip to content

Latest commit

 

History

History
66 lines (53 loc) · 1.5 KB

README.md

File metadata and controls

66 lines (53 loc) · 1.5 KB

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"