Giter Site home page Giter Site logo

martinheidegger / run-each Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 7 KB

A very small, flexible, parallel async iteration helper that has first-class support for Iterators and concurrency.

License: MIT License

JavaScript 100.00%
javascript async iterator iterable parallel callback each concurrency

run-each's Introduction

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

run-each's People

Contributors

martinheidegger avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.