Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

map-each

1.0.0 • Public • Published

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

Install

npm i map-each

DownloadsWeekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

15.6 kB

Total Files

11

Last publish

Collaborators

  • avatar