@rondymesquita/flow
TypeScript icon, indicating that this package has built-in type declarations

0.15.0 • Public • Published

flow

Simply execute functions in sequence.

  • Easy exception handling
  • Improve code readability
const { run } = flow([() => 1, () => 2])

const results = run()
// [
// 	{
// 		data: 1,
// 		status: 'OK',
// 	},
// 	{
// 		data: 2,
// 		status: 'OK',
// 	},
// ]

Stop execution

Simply make stage thrown an exception.

const { run } = flow([
  () => {
    throw new Error('sample error')
  },
  () => {
    return 'This stage will no be executed'
  },
])

const results = run()
// [
//   {
//     data: 'sample error',
//     status: 'FAIL',
//   }
// ]

Keep executing even with exception

Pass stopOnError option to run function.

const { run } = flow([
  () => {
    throw new Error('error')
  },
  () => 'this will be executed normally',
])

const results = run([stopOnError(false)])
// {
//   data: 'error',
//   status: 'FAIL',
// },
// {
//   data: 'this will be executed normally',
//   status: 'OK',
// },

Get all results or errors

const { run } = flow([
  () => {
    throw new Error('error')
  },
  () => 'this will be executed normally',
])

const results = run([stopOnError(false)])

const successes = results.filter((result) => result.status === Status.OK)
const errors = results.filter((result) => result.status === Status.FAIL)

Context

Share data between stages.

const { run } = flow([
  (ctx) => {
    ctx.set('color', 'red')
  },
  (ctx) => {
    ctx.get('color') // red
    ctx.set('mode', 'dark')
  },
  (ctx) => {
    ctx.get('color') // red
    ctx.get('mode') // dark
  },
])

const results = run()

Inject data in context before running.

const { run, context } = flow([
  (ctx: Context) => {
    ctx.get('mode') // light
  },
])

context.set('mode', 'light')
const results = run()

Provide custom arguments

Inject data on stages.

const { run, provideArgs } = flow([
  (pirate, ctx) => {
    // pirate.name is 'Jack Sparrow'
  },
])
provideArgs((ctx: Context) => {
  const pirate = { name: 'Jack Sparrow' }
  return [pirate, ctx]
})
const results = run()

Promises

If any stage resolves to a promise, use runAsync.

const { runAsync } = flow([
  async () => {
    await fetch()
  },
  () => {
    return Promise.resolve()
  },
  () => {
    return new Promise(...)
  },
])

const results = await runAsync()

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.15.01latest

Version History

VersionDownloads (Last 7 Days)Published
0.15.01
0.14.00
0.13.00
0.12.00
0.11.00
0.10.00
0.9.01
0.8.00
0.7.00
0.6.00
0.5.00
0.4.00

Package Sidebar

Install

npm i @rondymesquita/flow

Weekly Downloads

2

Version

0.15.0

License

MIT

Unpacked Size

23.9 kB

Total Files

15

Last publish

Collaborators

  • rondymesquita