0.15.0 • Published 3 months ago

@rondymesquita/flow v0.15.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

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()
0.15.0

3 months ago

0.14.0

11 months ago

0.13.0

11 months ago

0.12.0

11 months ago

0.11.0

11 months ago

0.10.0

11 months ago

0.9.0

11 months ago

0.8.0

11 months ago

0.7.0

11 months ago

0.6.0

11 months ago

0.5.0

11 months ago

0.4.0

12 months ago