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 --save chainy-plugin-each
- Module:
require('chainy-plugin-each')
- Install:
npm install --save chainy-plugin-each
- Module:
require('chainy-plugin-each')
- CDN URL:
//wzrd.in/bundle/[email protected]
- Install:
ender add chainy-plugin-each
- Module:
require('chainy-plugin-each')
This package is published with the following editions:
chainy-plugin-each
aliaseschainy-plugin-each/source/index.js
chainy-plugin-each/source/index.js
is Source + ES5 + Require
Discover the release history by heading on over to the HISTORY.md
file.
Discover how you can contribute by heading on over to the CONTRIBUTING.md
file.
These amazing people are maintaining this project:
No sponsors yet! Will you be the first?
No contributors yet! Will you be the first?
Discover how you can contribute by heading on over to the CONTRIBUTING.md
file.
Unless stated otherwise all works are:
- Copyright © 2014+ Bevry Pty Ltd
and licensed under: