AsyncIterator
This library is designed to implement a sequential iteration with the ability to use async functions within iterative functions.
Examples
let AsyncIterator = require('asynciteratorjs')
let sleep = ms => {
return new Promise(resolve => { setTimeout(resolve, ms) })
}
async function main() {
let iterator = new AsyncIterator([1, 2, 3, 4])
let array = []
await iterator.iterate(async (value, key, dataset) => {
await sleep(value * 1000)
array.push({ key, value })
})
// After 10 seconds (because of sleep function)
console.log(array) // [{ key: 0, value: 1 }, { key: 1, value: 2 }, { key: 2, value: 3 }, { key: 3, value: 4 }]
}
main()
Docs
new AsyncIterator(Elements)
Constructor of Async Iterator
Param | Type | Description |
---|---|---|
Elements | Array |
Dataset array |
asyncIterator.push(Elements)
Pushing new elements to iterator data storage
Kind: instance method of AsyncIterator
Param | Type | Description |
---|---|---|
Elements | Array |
New dataset array |
asyncIterator.reset()
This function is resetting data storage
Kind: instance method of AsyncIterator
asyncIterator.iterate(func, another)
This function is iterating dataset
Kind: instance method of AsyncIterator
Param | Type | Default | Description |
---|---|---|---|
func | Promise |
Iteration function | |
another | Boolean |
false |
Private variable |
Iteration function(value, key, array)
Param | Type | Description |
---|---|---|
value | Any |
Iteration value |
key | Number |
Iteration key |
array | Array |
Iteration dataset |