run-each
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

run-each

Build Status JavaScript Style Guide Maintainability Test Coverage

run-each is a very small, flexible, parallel async iteration 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 run-each --save

It is similar to each, async-each, each-async, ... and many other.

const { runEach } = require('run-each')
 
runEach(
  [
    function (callback) {
      callback()
    },
    function (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.

runEach([
  function (callback) {
    callback('a')
  },
  function (callback) {
    callback('b')
  }
], function (err, data) {
  Array.from(data) === ['a', '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.

runEach([], function () {
  console.log(arguments.length) // 1
})
 
runEach([], 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.

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

Concurrency

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

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

License

MIT

Dependencies (1)

Dev Dependencies (3)

Package Sidebar

Install

npm i run-each

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

14.5 kB

Total Files

11

Last publish

Collaborators

  • leichtgewicht