map-each
A very small, flexible, parallel async mapping helper that has first-class support for Iterators and concurrency.
Last updated 2 years ago by leichtgewicht .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install map-each 
SYNC missed versions from official npm registry.

map-each

Build Status JavaScript Style Guide Maintainability Test Coverage

map-each is a very small, flexible, parallel async mapping helper that has first-class support for Iterators (unlike other libraries, which mostly break with iterators) and concurrency. It also has complete TypeScript header files for comfortable integration.

npm i map-each --save

It is similar to async-iterate, now-and-later, async-parallel, ... and many other.

const { mapEach } = require('map-each')

mapEach(
  ['a', 'b', 'c']
  function (input, callback) {
    callback()
  },
  function (err) {
    // Done once all functions are done
  }
)

There are several things that make this different:

  • It returns an Iterable object instead of an Array.
  • It supports Promises in case you prefer to use async/await with your API's
  • It supports concurrency limits to limit the amount of commands executed at the same time.

Iterable results

Like in other libraries you can get the result, but unlike other libraries, its not an Array, so you need to apply an Array.from to it.

mapEach([
  ['a', 'b']
  function (item, callback) {
    callback(null, 'name:' + item)
  },
], function (err, data) {
  Array.from(data) === ['name:a', 'name:b'] // the order is protected
})

Note: This is more of an internal detail, but if the passed-in function doesn't have a second parameter, the data will not be collected.

mapEach([], function () {}, function () {
  console.log(arguments.length) // 1
})

mapEach([], function () {}, function (err, data) {
  console.log(arguments.length) // 2
})

Promise support

If you don't pass in a callback handler at the end, it will automatically return a Promise.

mapEach([/*...*/], function () {}).then(function () {
  // all done
})

Concurrency

By passing a concurrency limit to the runner, it will limit the amount of parallel executions

mapEach([/*...*/], function () {}, 1).then(function () {
  // now each operation is run in series.
})
mapEach([/*...*/], function () {}, 2).then(function () {
  // now two operations are run at the same time
})

License

MIT

Current Tags

  • 1.0.0                                ...           latest (2 years ago)

1 Versions

  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |