0.3.0 • Published 7 years ago
fast-iterator v0.3.0
fast-iterator
Fast execution of an array of functions with the same value as input that can be altered.
Install
npm i fast-iterator --save
Usage
const fast = require('fast-iterator')
fast(
[fn1, fn2, fn3], // the array of functions to execute
{ context: true } // a custom context for every function
)(
iterator.bind({ a: 'a', b: 'b' }), // an iterator function to pass custom parameters
{ hello: 'world' }, // the value to update
done // the function to call once the work has been done
)
function fn1 (a, b, result, done) {
console.log(a, b, result, this)
done(null, { ciao: 'mondo' })
}
function fn2 (a, b, result, done) {
console.log(a, b, result, this)
done(null, { winter: 'is coming' })
}
async function fn3 (a, b, result) {
console.log(a, b, result, this)
return { winter: 'has come' }
}
function iterator (fn, result, done) {
return fn(this.a, this.b, result, done)
}
function done (err, result) {
console.log(err || result, this)
}
Output:
a b { hello: 'world' } { context: true }
a b { ciao: 'mondo' } { context: true }
a b { winter: 'is coming' } { context: true }
{ winter: 'has come' } { context: true }
If you need to release the internal Object holder for performances reasons, you can call the fourth parameter of the iterator: release.
function iterator (fn, result, done, release) {
if (someCondition) return release()
return fn(this.a, this.b, result, done)
}
Acknowledgements
This project is kindly sponsored by LetzDoIt.
License
Licensed under MIT.