1.0.2 • Published 2 years ago

@meech3/async-array v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

@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

  1. Install npm
  2. npm install @meech3/async-array

Table of Contents

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 // series

Methods

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); // false

pushAsync(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));
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago