Skip to content
/ each Public

Chainy action that iterates through each item in the array with an asynchronous or synchronous iterator

License

Notifications You must be signed in to change notification settings

chainyjs/each

Repository files navigation

Each action for ChainyJS

Travis CI Build Status NPM version NPM downloads Dependency Status Dev Dependency Status
Slack community badge Patreon donate button Gratipay donate button Flattr donate button PayPal donate button Bitcoin donate button Wishlist browse button

Chainy action that iterates through each item in the array with an asynchronous or synchronous iterator

Use the each plugin like so:

require('chainy').create().require('set each')
	// Iterate over an array
	.set(['a', 'b', 'c'])
	.each(iterator)

	// Iterate over an object
	.set({a: 1, b: 2, c: 3})
	.each(iterator)

	// Iterate with some custom options to use for the internal taskgroup
	.each(iterator, taskgroupOptions)

The iterator can operate synchronously:

.each(function (itemValue) {
	// ..
})

Or asynchronously:

.each(function (itemValue, complete) {
	// ...
	complete()
})

Or asynchronously with the item's index/key as well:

.each(function (itemValue, itemIndex, complete) {
	// ...
	complete()
})

The taskgroupOptions is an optional configuration object that can be used to configure the internal taskgroup. A common use case for this is to instead of having our iterators operate serially (one after the other, a concurrency of 1), instead we could have them all operate at once (with a concurrency of 0) by specifying the following taskgroup configuration like so:

.each(iterator, {concurrency: 0})

Tying this all together, we can do things like:

require('chainy').create().require('set each')

	// --------------------------------
	// Array data
	.set(['a', 'b', 'c'])

	// Synchronous iterator
	.each(function (itemValue) {
		console.log(itemValue)  // 'a' then 'b' then 'c'
	})

	// Asynchronous iterator
	.each(function (itemValue, complete) {
		console.log(itemValue)  // 'a' then 'b' then 'c'
		complete()
	})

	// Asynchronous iterator with index
	.each(function (itemValue, itemIndex, complete) {
		console.log(itemValue)  // 'a' then 'b' then 'c'
		console.log(itemIndex)  // 0 then 1 then 2
		complete()
	})


	// --------------------------------
	// Object data
	.set({a:1, b:2, c:3})

	// Synchronous iterator
	.each(function (itemValue) {
		console.log(itemValue)  // 1 then 2 then 3
	})

	// Asynchronous iterator
	.each(function (itemValue, complete) {
		console.log(itemValue)  // 1 then 2 then 3
		complete()
	})

	// Asynchronous iterator with index
	.each(function (itemValue, itemIndex, complete) {
		console.log(itemValue)  // 1 then 2 then 3
		console.log(itemIndex)  // 'a' then 'b' then 'c'
		complete()
	})

Install

NPM

  • Install: npm install --save chainy-plugin-each
  • Module: require('chainy-plugin-each')

Browserify

  • Install: npm install --save chainy-plugin-each
  • Module: require('chainy-plugin-each')
  • CDN URL: //wzrd.in/bundle/[email protected]

Ender

  • Install: ender add chainy-plugin-each
  • Module: require('chainy-plugin-each')

This package is published with the following editions:

  • chainy-plugin-each aliases chainy-plugin-each/source/index.js
  • chainy-plugin-each/source/index.js is Source + ES5 + Require

History

Discover the release history by heading on over to the HISTORY.md file.

Contribute

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Backers

Maintainers

These amazing people are maintaining this project:

Sponsors

No sponsors yet! Will you be the first?

Patreon donate button Gratipay donate button Flattr donate button PayPal donate button Bitcoin donate button Wishlist browse button

Contributors

No contributors yet! Will you be the first?

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

License

Unless stated otherwise all works are:

and licensed under:

About

Chainy action that iterates through each item in the array with an asynchronous or synchronous iterator

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published