Create chainable, reusable hooks to control the flow.
yarn add flow-hooks
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';
},
]);
git commit --allow-empty -m "Release 1.0.1"