1.0.2 • Published 7 years ago
@wessberg/iteration v1.0.2
Iteration
A class that helps with iterating over asynchronous content.
Installation
Simply do: npm install @wessberg/iteration
.
Usage
const iterator = new Iteration();
const collection = [1, 2, 3];
// Parallel filtering of a collection.
const result1 = await iterator.filterParallel(collection, async item => {
await wait();
return item % 2 === 0;
});
console.log(result1) // [2]
// Sequentially mapping of a collection
const result2 = await iterator.map(collection, async item => {
await wait();
return item * 2;
});
console.log(result2) // [2, 4, 6]
// Parallel forEach iteration of a collection
await iterator.forEachParallel(collection, async item => {
await wait();
doStuff();
});
// Perform an operation 10 times in parallel.
await iterator.forParallel(10, async () => {
doStuff();
});
let truthValue = false;
const condition = () => truthValue;
// Iterates forever until the condition returns a falsy value
// or a Promise that resolves with a falsy value.
await iterator.while(condition, async () => {
doStuff();
if (something) truthValue = false;
});
Changelog:
v1.0.1:
- Refactoring. Updated type signature of 'map' and 'mapParallel' to support inferred return types.
v1.0.1:
- Type definition updates. Bumped Typescript dependency to ^2.4.0
v1.0.0:
- First release.