@meech3/async-array v1.0.2
@meech3/async-array
Provides an AsyncArray class with parallel and series asynchronous versions of common array methods.
All native array methods are still available on an AsyncArray object.
Installation
- Install npm
npm install @meech3/async-array
Table of Contents
- Getting started
- Naming convention
- Methods
- toArray
- isAsyncArray(arr)
- pushAsync(callback)
- unshiftAsync(callback)
- forEachAsync(callback)
- forEachAsyncSeries(callback)
- mapAsync(callback)
- mapSeries(callback)
- filterAsync(callback)
- filterAsyncSeries(callback)
- reduceAsync(callback, [start], [end])
- fillAsync(callback, [start], [end])
- fillAsyncSeries(callback, [start], [end])
- Example usage
Getting Started
To use the package in your project you can import it like this:
import AsyncArray from '@meech3/async-array';To create a new AsyncArray call the contructor with your array elements:
const arr = new AsyncArray(1, 2, 3);Naming Convention
For a native array method called funcName, the async method names (if they exist) will be:
funcNameAsync // parallel
funcNameSeries // seriesMethods
toArray
Converts an AsyncArray to a normal array
const arr = asyncArr.toArray();isAsyncArray(arr)
Determines whether the passed value is an AsyncArray
const arr = [1, 2, 3];
AsyncArray.isAsyncArray(arr); // falsepushAsync(callback)
Adds a new item to the end of an array
await arr.pushAsync(async () => await doSomethingAsync());Note: To push multiple values return an array of values from the callback
unshiftAsync(callback)
Adds a new item to the end of an array
await arr.pushAsync(async () => await doSomethingAsync());Note: To add multiple values return an array of values from the callback
forEachAsync(callback)
Calls an async function for each element in the array in parallel
await arr.forEachAsync(async () => await doSomethingAsync());forEachSeries(callback)
Calls an async function for each element in the array in series
await arr.forEachSeries(async () => await doSomethingAsync());mapAsync(callback)
Creates a new array from calling an async function for every array element in parallel
const result = await arr.mapAsync(async () => await doSomethingAsync());mapSeries(callback)
Creates a new array from calling an async function for every array element in series
const result = await arr.mapSeries(async () => await doSomethingAsync());filterAsync(callback)
Creates a new array filled with elements that pass a test provided by a function in parallel
const result = await arr.filterAsync(async () => await doSomethingAsync());filterSeries(callback)
Creates a new array filled with elements that pass a test provided by a function in series
const result = await arr.filterSeries(async () => await doSomethingAsync());reduceAsync(callback, start, end)
Executes an async reducer function for each array element
const result = await arr.reduceAsync((prev, curr) => {
return await doSomethingAsync();
}, 1, 3);fillAsync(callback, start, end)
Fills specified elements in an array with a value in parallel
await arr.fillAsync(async (element, index) => {
return await doSomethingAsync();
}, 1, 3);fillSeries(callback, start, end)
Fills specified elements in an array with a value in series
await arr.fillSeries(async (element, index) => {
return await doSomethingAsync();
}, 1, 3);Example Usage
const gameIds = new AsyncArray(1, 2, 3);
const highScores = await gameIds.filterAsync(async (id) => {
const score = await getScore(id);
return score > 100;
});
const players = await gameIds.mapAsync(async id => await getPlayers(id));