GithubHelp home page GithubHelp logo

tunnckocore / each-promise Goto Github PK

View Code? Open in Web Editor NEW
26.0 3.0 1.0 553 KB

:cyclone: Asynchronous control flow library for now and then. :sparkles: Iterate over promises, promise-returning or async/await functions in series or parallel. Works on node 0.10 if you give it a Promise.

License: MIT License

JavaScript 100.00%
promises async control control-flow each parallel concurrent series serial concurrency

each-promise's Introduction

each-promise npm version npm version npm downloads monthly npm downloads total
Each Promise - Async control flow library
Asynchronous control flow library

Iterate over promises, promise-returning or async/await functions in series or parallel. Support settle (fail-fast), concurrency (limiting) and hooks system (start, beforeEach, afterEach, finish)

codeclimate codestyle linux build windows build codecov dependency status

Table of Contents

(TOC generated by verb using markdown-toc)

Install

Install with npm

$ npm install each-promise --save

or install using yarn

$ yarn add each-promise

Usage

For more use-cases see the tests

const eachPromise = require('each-promise')
const arr = [
  123,
  'foo',
  () => 456,
  Promise.resolve(567)
  false,
  () => Promise.resolve(11)
]

eachPromise
  .serial(arr)
  .then((res) => {
    console.log(res) // => [123, 'foo', 456, 567, false, 11]
  })

Background

You may think why this exists, what is this for, why not Sindre's microlibs like p-map, p-map-series, p-settle, p-each-series or p-reduce.

Why not "promise fun"?

They do their jobs okey, but in some cases they don't. And that's the my case. I need control over "fast fail" behavior, also known as "settle" or "bail". I need serial and parallel iteration, but parallel with concurrency too. They requires node v4, and uses native Promise constructor. I believe in that we should not use modern things if we don't need them, it is just syntax sugar. This package is written in way that works in node versions below v4 and also you can pass custom Promise constructor through options.Promise if you want.

  • node@4 required
  • no hooks system
  • no settle / fail-fast / bail
  • no custom Promise
  • no real and meaningful tests
  • concurrency control

back to top

Why not separate libs?

Why not separate .serial and .parallel into own libs like Sindre did? Because the main core logic and difference is absolutely in just 2-3 lines of code and one if check. The main thing is that parallel uses for loop with concurrency combination, and series does not use loops, but recursive function calls.

For free you get hooks system. And really it cost nothing. It just able to be done, because the structure of the code and because I need such thing.

  • node v0.10 and above
  • custom Promise constructor
  • real settle / fail fast
  • hook system, through options
  • very stable and well tested with real tests
  • concurrency control

back to top

API

Iterate over iterable in series (serially) with optional opts (see options section) and optional mapper function (see item section).

Params

  • <iterable> {Array}: iterable object like array with any type of values
  • [mapper] {Function}: function to apply to each item in iterable, see item section
  • [opts] {Object}: see options section
  • returns {Promise}: Always resolved or rejected promise

Example

var delay = require('delay')
var eachPromise = require('each-promise')

var arr = [
  () => delay(500).then(() => 1),
  () => delay(200).then(() => { throw Error('foo') }),
  () => delay(10).then(() => 3),
  () => delay(350).then(() => 4),
  () => delay(150).then(() => 5)
]

eachPromise
  .serial(arr)
  .then((res) => {
    console.log(res) // [1, Error: foo, 3, 4, 5]
  })

// see what happens when parallel
eachPromise
  .parallel(arr)
  .then((res) => {
    console.log(res) // => [3, 5, Error: foo, 4, 1]
  })

// pass `settle: false` if you want
// to stop after first error
eachPromise
  .serial(arr, { settle: false })
  .catch((err) => console.log(err)) // => Error: foo

Iterate concurrently over iterable in parallel (support limiting with opts.concurrency) with optional opts (see options section) and optional mapper function (see item section).

Params

  • <iterable> {Array}: iterable object like array with any type of values
  • [mapper] {Function}: function to apply to each item in iterable, see item section
  • [opts] {Object}: see options section
  • returns {Promise}: Always resolved or rejected promise

Example

var eachPromise = require('each-promise')

var arr = [
  function one () {
    return delay(200).then(() => {
      return 123
    })
  },
  Promise.resolve('foobar'),
  function two () {
    return delay(1500).then(() => {
      return 345
    })
  },
  delay(10).then(() => 'zero'),
  function three () {
    return delay(400).then(() => {
      coffffnsole.log(3) // eslint-disable-line no-undef
      return 567
    })
  },
  'abc',
  function four () {
    return delay(250).then(() => {
      return 789
    })
  },
  function five () {
    return delay(100).then(() => {
      sasasa // eslint-disable-line no-undef
      return 444
    })
  },
  function six () {
    return delay(80).then(() => {
      return 'last'
    })
  }
]

// does not stop after first error
// pass `settle: false` if you want
eachPromise
  .parallel(arr)
  .then((res) => {
    console.log(res)
    // => [
    //   'foobar',
    //   'abc',
    //   'zero',
    //   'last',
    //   ReferenceError: sasasa is not defined,
    //   123,
    //   789,
    //   ReferenceError: coffffnsole is not defined
    //   345
    // ]
  })

Iterate over iterable in series or parallel (default), depending on default opts. Pass opts.serial: true if you want to iterate in series, pass opts.serial: false or does not pass anything for parallel.

Params

  • <iterable> {Array}: iterable object like array with any type of values
  • [mapper] {Function}: function to apply to each item in iterable, see item section
  • [opts] {Object}: see options section
  • returns {Promise}: Always resolved or rejected promise

Example

var delay = require('delay')
var eachPromise = require('each-promise')

var arr = [
  123,
  function () {
    return delay(500).then(() => 456)
  },
  Promise.resolve(678),
  function () {
    return 999
  },
  function () {
    return delay(200).then(() => 'foo')
  }
]

eachPromise
  .each(arr)
  .then(function (res) {
    console.log('done', res) // => [123, 678, 999, 'foo', 456]
  })

Options

You have control over everything, through options.

  • Promise {Function}: custom Promise constructor to be used, defaults to native
  • mapper {Function}: function to apply to each item in iterable, see item section
  • settle {Boolean}: if false stops after first error (also known as "fail-fast" or "bail"), default true
  • flat {Boolean}: result array to contain only values, default true
  • concurrency {Number}: works only with .parallel method, defaults to iterable length
  • start {Function}: on start hook, see hooks section
  • beforeEach {Function}: called before each item in iterable, see hooks section
  • afterEach {Function}: called after each item in iterable, see hooks section
  • finish {Function}: called at the end of iteration, see hooks section
  • context {Object}: custom context to be passed to each fn in iterable
  • args {Array}: custom argument(s) to be pass to fn, given value is arrayified

back to top

Hooks

You can do what you want between stages through hooks - start, before each, after each, finish.

  • start {Function}: called at the start of iteration, before anything
  • beforeEach {Function}: passed with item, index, arr arguments
    • item is an object with value, reason and index properties, see item section
    • index is the same as item.index
    • arr is the iterable object - array or object
  • afterEach {Function}: passed with item, index, arr arguments
    • item is an object with value, reason and index properties, see item section
    • index is the same as item.index
    • arr is the iterable object - array or object
  • finish {Function}: called at the end of iteration, see finish hook section

back to top

Item

That object is special object, that is passed to beforeEach and afterEach hooks, also can be found in result object if you pass opts.flat: false option. And passed to opts.mapper function too.

  • item.value resolved/rejected promise value, if at beforeEach hook it can be function
  • item.reason may not exist if item.value, if exist it is standard Error object
  • item.index is number, order of "executing", not the order that is defined in iterable

back to top

Finish hook

This hooks is called when everything is finished / completed. At the very end of iteration. It is passed with err, result arguments where:

  • err is an Error object, if opts.settle: false, otherwise null
  • result is always an array with values or item objects if opts.flat: false

back to top

Related

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Please read the contributing guidelines for advice on opening issues, pull requests, and coding standards.
If you need some help and can spent some cash, feel free to contact me at CodeMentor.io too.

In short: If you want to contribute to that project, please follow these things

  1. Please DO NOT edit README.md, CHANGELOG.md and .verb.md files. See "Building docs" section.
  2. Ensure anything is okey by installing the dependencies and run the tests. See "Running tests" section.
  3. Always use npm run commit to commit changes instead of git commit, because it is interactive and user-friendly. It uses commitizen behind the scenes, which follows Conventional Changelog idealogy.
  4. Do NOT bump the version in package.json. For that we use npm run release, which is standard-version and follows Conventional Changelog idealogy.

Thanks a lot! :)

Building docs

Documentation and that readme is generated using verb-generate-readme, which is a verb generator, so you need to install both of them and then run verb command like that

$ npm install verbose/verb#dev verb-generate-readme --global && verb

Please don't edit the README directly. Any changes to the readme must be made in .verb.md.

Running tests

Clone repository and run the following in that cloned directory

$ npm install && npm test

Author

Charlike Mike Reagent

Logo

The logo is Cyclone Emoji from EmojiOne.com. Released under the CC BY 4.0 license.

License

Copyright © 2016-2018, Charlike Mike Reagent. Released under the MIT License.


This file was generated by verb-generate-readme, v0.4.3, on March 19, 2017.
Project scaffolded using charlike cli.

each-promise's People

Contributors

greenkeeper[bot] avatar greenkeeperio-bot avatar tunnckocore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

each-promise's Issues

An in-range update of redolent is breaking the build 🚨

Version 2.0.5 of redolent just got published.

Branch Build failing 🚨
Dependency redolent
Current Version 2.0.4
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

As redolent is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪


Status Details
  • ci/circleci Your tests passed on CircleCI! Details

  • continuous-integration/travis-ci/push The Travis CI build passed Details

  • continuous-integration/appveyor/branch AppVeyor build failed Details

Commits

The new version differs by 5 commits .

  • 59fd760 chore(release): 2.0.5
  • 54ac357 fix(ci): add CircleCI yaml
  • b390b23 fix(services): add .eslintrc instead, so CodeClimate will respect it
  • 074a728 fix(deps): update deps, add eslintConfig field
  • 67ae2b6 fix(returns): should return rejected error when non Promise value

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of redolent is breaking the build 🚨

Version 2.0.3 of redolent just got published.

Branch Build failing 🚨
Dependency redolent
Current Version 2.0.2
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

As redolent is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/appveyor/branch Waiting for AppVeyor build to complete Details

  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 2 commits .

  • 229bbf2 chore(release): 2.0.3
  • c465abe fix(deps): dont resolve "native-or-another" and ES6-ify

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

finish hook not called when settle: false and has failing item

nah, probably because https://github.com/tunnckoCore/each-promise/blob/master/utils.js#L73 is instabul ignored and not test, heh.

WHOAAH!! Found and fixed locally!

      function onFulfilled () {
        if (arr.doneCount++ === arr.length - 1) {
          options.finish(null, results)
          resolve(results)
          return
        }
        next(index + options.concurrency)
      }

      function onRejected (err) {
        if (options.settle === false) {
          options.finish(err, results)
          reject(err)
        }
      }

      promise
        .then(handle('value'), handle('reason'))
        .catch(onRejected) // this mate here
        .then(onFulfilled, onRejected)
        .catch(onRejected)

Consider how to handle if some is not finished

It even not calls finish hook when in serial mode.

Say you have 5 functions in array. Say the 3rd one is using cb to be considered done and others returns promises, so when this cb isn't called next functions in the stack is not called too

var arr = [
  () => delay(900).then(() => 1),
  () => delay(620).then(() => 2),
  function sasa (cb) {
    // cb()
  },
  () => delay(400).then(() => 4),
  () => delay(700).then(() => 5),
]

maybe it can be fixed in https://github.com/hybridables/try-catch-callback

Improve one test: parallel + settle:false

It's not correct. It no matter that .catch is called. In parallel mode, currently, "fail fast" more means "call .catch when fail-fast, otherwise don't call .catch, but always then()", instead of "stop executing the other tests after the first error"

example backup

// var foo = 'initial'
// var arr = [
//   function one () {
//     return delay(200).then(() => {
//       // console.log(11)
//       return 123
//     })
//   },
//   Promise.resolve('foobar'),
//   function two () {
//     return delay(1500).then(() => {
//       // console.log(22)
//       foo += '~third'
//       return 345
//     })
//   },
//   delay(10).then(() => 'zero'),
//   function three () {
//     return delay(400).then(() => {
//       // console.log(33, foo)
//       coffffnsole.log(3) // eslint-disable-line no-undef
//       return 567
//     })
//   },
//   'abc',
//   function four () {
//     return delay(250).then(() => {
//       console.log(44)
//       return 789
//     })
//   },
//   function five () {
//     return delay(100).then(() => {
//       sasasa // eslint-disable-line no-undef
//       // console.log(55)
//       return 444
//     })
//   },
//   function six () {
//     return delay(80).then(() => {
//       console.log(66)
//       return 'last'
//     })
//   }
// ]

// // .serial/.series:
// // - works correctly
// // .parallel:
// // - ?? (not sure) bug when `settle: false` and `concurrency < arr.length`
// //
// var settle = false
// each.parallel(arr/*, function mapper (item, index) {
//   console.log(item, index)
// } */, {
//   concurrency: 5,
//   settle: settle,
//   start: function () {
//     console.log('start')
//     console.log('====')
//   },
//   finish: function (err, res) {
//     console.log('====')
//     console.log('finish')
//     if (err) console.log('err:', err)
//     console.log('res:', res, res.length)
//   },
//   beforeEach: function (item, index) {
//     if (index === 2) foo += '~beforeEach'
//     if (index === 3) foo = 'reset'
//     console.log('beforeEach', index)
//   },
//   afterEach: function (item, index) {
//     if (item.reason) {
//       console.log('error', index, item.reason)
//     }
//     if (index === 3) foo = 'reset'
//     console.log('afterEach', index, foo === 'initial~beforeEach~third')
//   }
// })
// .then(function (res) {
//   console.log('ok')
// })
// .catch(function (er) {
//   console.log('fail')
//   if (settle === false) process.exit(0)
// })

An in-range update of nyc is breaking the build 🚨

Version 10.2.0 of nyc just got published.

Branch Build failing 🚨
Dependency nyc
Current Version 10.1.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As nyc is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/appveyor/branch Waiting for AppVeyor build to complete Details

  • continuous-integration/travis-ci/push The Travis CI build could not complete due to an error Details

Commits

The new version differs by 6 commits .

  • 455619f chore(release): 10.2.0
  • 95cc09a feat: upgrade to version of yargs with extend support (#541)
  • 43535f9 chore: explicit update of istanbuljs dependencies (#535)
  • 98ebdff feat: allow babel cache to be enabled (#517)
  • 50adde4 feat: exclude the coverage/ folder by default 🚀 (#502)
  • 6a59834 chore(package): update tap to version 10.0.0 (#507)

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of nyc is breaking the build 🚨

Version 11.0.3 of nyc just got published.

Branch Build failing 🚨
Dependency nyc
Current Version 11.0.2
Type devDependency

This version is covered by your current version range and after updating it in your project the build failed.

As nyc is “only” a devDependency of this project it might not break production or downstream projects, but “only” your build or test tools – preventing new deploys or publishes.

I recommend you give this issue a high priority. I’m sure you can resolve this 💪

Status Details
  • ci/circleci Your tests passed on CircleCI! Details
  • continuous-integration/travis-ci/push The Travis CI build failed Details
  • continuous-integration/appveyor/branch AppVeyor build failed Details

Commits

The new version differs by 4 commits.

  • e9fad9f docs: add note about tap-nyc to README (#614)
  • f86b0b1 chore(release): 11.0.3
  • 923b062 fix: upgrade to spawn-wrap version that works with babel-register (#617)
  • b1eb4d6 fix: update help link to list of reporters (#601)

See the full diff

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

An in-range update of redolent is breaking the build 🚨

Version 2.0.4 of redolent just got published.

Branch Build failing 🚨
Dependency redolent
Current Version 2.0.3
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

As redolent is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪


Status Details
  • ci/circleci Your tests passed on CircleCI! Details

  • continuous-integration/travis-ci/push The Travis CI build passed Details

  • continuous-integration/appveyor/branch AppVeyor build failed Details

Commits

The new version differs by 4 commits .

  • 05d8745 chore(release): 2.0.4
  • 3577762 fix(ci): appveyor-retry
  • 181f41b fix(tweak): tests
  • 631823a fix(return): handle when done is present, but return promise

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

use try-catch-core?

utils.iterator = function iterator (arr, results) {
  return function (options, resolve, reject) {
    return function next (index) {
      if (index >= arr.length) {
        return
      }

      var item = arr[index]
      options.beforeEach({ value: item, index: index }, index, arr)

      // changed
      var promise = null

      if (typeof item === 'function') {
        promise = options.Promise(function (resolve, reject) {
          utils.tryCatchCore(item, options, function (err, res) {
            if (err) {
              return reject(err)
            }
            if (res instanceof Error) {
              return reject(res)
            }
            resolve(res)
          })
        })
      } else {
        promise = item instanceof Error
          ? options.Promise.reject(item)
          : options.Promise.resolve(item)
      }
      // changed

      var handle = utils.handleResults({
        arr: arr,
        index: index,
        results: results
      }, options)

      promise
        .then(handle('value'), handle('reason'))
        .then(function onresolved () {
          if (arr.doneCount++ === arr.length - 1) {
            options.finish(null, results)
            resolve(results)
            return
          }
          next(index + options.concurrency)
        }, function onrejected (err) {
          if (options.settle === false) {
            options.finish(err, results)
            reject(err)
            return
          }
        })
    }
  }
}

An in-range update of redolent is breaking the build 🚨

Version 2.0.2 of redolent just got published.

Branch Build failing 🚨
Dependency redolent
Current Version 2.0.1
Type dependency

This version is covered by your current version range and after updating it in your project the build failed.

As redolent is a direct dependency of this project this is very likely breaking your project right now. If other packages depend on you it’s very likely also breaking them.
I recommend you give this issue a very high priority. I’m sure you can resolve this 💪


Status Details
  • continuous-integration/appveyor/branch Waiting for AppVeyor build to complete Details

  • continuous-integration/travis-ci/push The Travis CI build failed Details

Commits

The new version differs by 3 commits .

  • c5152d2 chore(release): 2.0.2
  • 56e97a4 fix(docs): update docs
  • b3edc4e fix(deps): switch to use "native-or-another"

See the full diff.

Not sure how things should work exactly?

There is a collection of frequently asked questions and of course you may always ask my humans.


Your Greenkeeper Bot 🌴

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.