Skip to content

Making simple iterator for [async][] lib that adds beforeEach, afterEach, error hooks and support for settling. It also emits `beforeEach`, `afterEach` and `error` events.

License

Notifications You must be signed in to change notification settings

tunnckoCore/async-simple-iterator

Making simple iterator for async lib that adds beforeEach, afterEach, error hooks and support for settling. It also emits beforeEach, afterEach and error events.

code climate standard code style travis build status coverage status dependency status

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

Initialize AsyncSimpleIterator with options.

Params

  • options {Object=}: Pass beforeEach, afterEach and error hooks or settle: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
})

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=}: Pass beforeEach, afterEach and error hooks or settle option.
  • returns {Function}: Wrapped iterator function which can be passed to every async method.

Events

  • emits: beforeEach with signature val[, value], next
  • emits: afterEach with signature err, res, val[, value], next
  • emits: error with signature err, 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

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.

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github

About

Making simple iterator for [async][] lib that adds beforeEach, afterEach, error hooks and support for settling. It also emits `beforeEach`, `afterEach` and `error` events.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •