Functional async flow control library built on promises. Managing promises and async code has never been easier.
- Small footprint
- Native promise support
- No chaining required
- Benchmarking (yes, even Promises)
- Logging (Chain logs, argument logs, and more...)
- Download the latest package
- NPM:
npm install breeze
Node.js / Browserify / Webpack
const Breeze = require('breeze')
import Breeze from 'breeze'
let flow = new Breeze()
Add step to flow chain.
flow
.then(value => 'function with return value')
.then(value => console.log('function says:', value))
.then(new Promise((resolve, reject) => {
return resolve('Promise resolution')
}))
.then(value => console.log('promise says:', value))
Note: You are not required to chain instance methods.
flow.then(value => 'function with return value')
flow.then(value => console.log('function says:', value))
Handle chain rejections. Accepts an optional custom error type to capture specific rejections in the flow chain.
flow.then(() => throw new Error('Spoiler Alert'))
flow.catch(CustomError, err => console.log('Specialized Catch:', err))
flow.catch(err => console.log('Generic Catch:', err))
Identify a step. Useful for benchmarking and logs.
// Create a flow step
flow.then(results => client.get('/users'))
// Identify step for benchmarking and logs
flow.id('fetch-users')
Invoke method on results of each Promise in the given Array.
Todo: Support previous chain Array<Promise>
value.
Map promise results to an array in order resolved.
Map promise results to an array in given order.
Skip n
steps after this point.
Obtain entry in array at given index in next step.
flow
.then(() => [1,2,3])
.get(0)
.then(item => console.log(item)) // Outputs: 1
Invokes method when the conditional
argument is truthy
, otherwise skips to the next step.
flow
.then(() => [1, 2, 3])
.when(result => result[0] === 1, result => console.log(result[0], '=', 1))
This is a basic example to illustrate the small power of how you can make if
statements
asynchronous.
Spreads each argument
from a successful step as individual arguments on the passed method
flow
.then(() => ['username', 'Steven Seagal'])
.spread((field, value) => console.log(field, '=', value)) // username = Steven Seagal
Invoke method
without modifying the return result of the step, useful for inspection.
flow
.then(() => [1, 2, 3])
.tap(result => console.log(result))
.then(result => console.log(result)) // [1,2,3]
Convenience method for:
.then(() => value)
Convenience method for:
.then(() => throw error)
Licensed under The MIT License.