async-simple-iterator
v0.1.5
Published
Making simple iterator for [async][] lib that adds beforeEach, afterEach, error hooks and support for settling. It also emits `beforeEach`, `afterEach` and `error` events.
Downloads
4
Readme
async-simple-iterator
Making simple iterator for async lib that adds beforeEach, afterEach, error hooks and support for settling. It also emits
beforeEach
,afterEach
anderror
events.
Install
npm i async-simple-iterator --save
Usage
For more use-cases see the tests
var base = require('async-simple-iterator')
// or get constructor
var AsyncSimpleIterator = require('async-simple-iterator').AsyncSimpleIterator
API
AsyncSimpleIterator
Initialize
AsyncSimpleIterator
withoptions
.
Params
options
{Object=}: PassbeforeEach
,afterEach
anderror
hooks orsettle:true
.
Example
var ctrl = require('async')
var AsyncSimpleIterator = require('async-simple-iterator').AsyncSimpleIterator
var fs = require('fs')
var base = new AsyncSimpleIterator({
settle: true,
beforeEach: function (val) {
console.log('before each:', val)
},
error: function (err, res, val) {
console.log('on error:', val)
}
})
var iterator = base.wrapIterator(fs.stat, {
afterEach: function (err, res, val) {
console.log('after each:', val)
}
})
ctrl.map([
'path/to/existing/file.js',
'filepath/not/exist',
'path/to/file'
], iterator, function (err, results) {
// => `err` will always be null, if `settle:true`
// => `results` is now an array of stats for each file
})
.wrapIterator
Wraps
iterator
function which then can be passed to async lib. You can pass returned iterator function to every async method that you want.
Params
iterator
{Function}: Iterator to pass to async lib.options
{Object=}: PassbeforeEach
,afterEach
anderror
hooks orsettle
option.returns
{Function}: Wrappediterator
function which can be passed to every async method.
Events
emits
:beforeEach
with signatureval[, value], next
emits
:afterEach
with signatureerr, res, val[, value], next
emits
:error
with signatureerr, res, val[, value], next
Example
var ctrl = require('async')
var base = require('async-simple-iterator')
base
.on('afterEach', function (err, res, value, key, next) {
console.log('after each:', err, res, value, key)
})
.on('error', function (err, res, value, key, next) {
console.log('on error:', err, res, value, key)
})
var iterator = base.wrapIterator(function (value, key, next) {
console.log('actual:', value, key)
if (key === 'dev') {
var err = new Error('err:' + key)
err.value = value
next(err)
return
}
next(null, 123 + key + 456)
}, {
settle: true,
beforeEach: function (value, key, next) {
console.log('before each:', value, key)
}
})
ctrl.forEachOf({
dev: './dev.json',
test: './test.json',
prod: './prod.json'
}, iterator, function done (err) {
// if settle:false, `err`
// if settle:true, `null`
console.log('end:', err)
})
Related
- async: Higher-order functions and common patterns for asynchronous code | homepage
- async-base-iterator: Basic iterator for async library that handles async and synchronous… more | homepage
- async-control: Ultimate asynchronous control flow goodness with built-in hook system and… more | homepage
- relike: Simple promisify a callback-style function with sane defaults. Support promisify-ing… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.