npm

Need private packages and team management tools?Check out npm Orgs. »

async-simple-iterator

0.1.5 • Public • Published

async-simple-iterator npmjs.com The MIT License

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

AsyncSimpleIterator

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
})

.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=}: 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.

Charlike Make Reagent new message to charlike freenode #charlike

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github

install

npm i async-simple-iterator

Downloadsweekly downloads

9

version

0.1.5

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability