Class: AsyncEach

AsyncEach()

The AsyncEach class implements a lazy, composable, asynchronous iterable interface.

Each instance is defined by its [Symbol.asyncIterator]() implementation.
It provides static and instance methods to construct, transform, and evaluate async sequences.

AsyncEach shares the same fluent API as What and Each, exposing the 9 core methods:

This enables declarative and composable pipelines over asynchronous sequences with lazy evaluation.

Constructor

new AsyncEach()

See:
Example
const seq = AsyncEach.of(
  Promise.resolve(1),
  Promise.resolve(2),
  Promise.resolve(3)
).sthen(x => x * 2);

for await (const value of seq.when()) {
  console.log(value); // 2, 4, 6
}

An iterable of Promises can be transformed and composed like any other iterable
using the methods of the Each class.  
Finally, Each.when can transform an iterable of Promises into an `AsyncEach` of resolved values,
allowing you to compose, restrict, and transform asynchronously even before the values are available.

Methods

each(thatopt) → {AsyncEach}

Cartesian product with another iterable.

Parameters:
Name Type Attributes Description
that Iterable | AsyncIterable | Array.<Promise> <optional>
Returns:
Type
AsyncEach

else(thatopt) → {AsyncEach}

Concatenates this AsyncEach with another iterable, or flattens it if omitted.

Parameters:
Name Type Attributes Description
that Iterable | AsyncIterable | Promise <optional>
Returns:
Type
AsyncEach

(async) equals(that) → {Promise.<boolean>}

Compares this AsyncEach with another iterable or AsyncEach for deep equality.

Parameters:
Name Type Description
that Iterable | AsyncIterable
Returns:

Resolves to true if equal

Type
Promise.<boolean>

if(predicate) → {AsyncEach}

Filters items using a predicate (sync or async).

Parameters:
Name Type Description
predicate function

A function returning boolean or Promise

Returns:

Filtered AsyncEach

Type
AsyncEach

match(thatopt) → {AsyncEach}

Zips this sequence with another. Mirrors Each.match.

Parameters:
Name Type Attributes Description
that Iterable | AsyncIterable | Array.<Promise> <optional>
Returns:
Type
AsyncEach

self() → {AsyncEach}

Returns a self-repeating iterable.

Returns:
Type
AsyncEach

sthen(fn) → {AsyncEach}

Maps a function over each item (sync or async).

Parameters:
Name Type Description
fn function

Mapping function

Returns:

Mapped AsyncEach

Type
AsyncEach

(async) toArray() → {Promise.<Array.<*>>}

Collects this AsyncEach into an array of resolved values.

Returns:

Resolves to an array

Type
Promise.<Array.<*>>

what(op, got) → {Promise.<*>}

Delegates to AsyncEach.what.

Parameters:
Name Type Description
op function
got *
Returns:
Type
Promise.<*>

when(popt, startopt, inclusiveopt) → {AsyncEach}

Delegates to AsyncEach.when.

  • If called without args, bridges sync iterables of Promises to async iterables of values.
  • If given a predicate or index, slices the sequence.
Parameters:
Name Type Attributes Default Description
p function | number <optional>
start boolean <optional>
true
inclusive boolean <optional>
start
Returns:
Type
AsyncEach

which(predicate) → {AsyncEach}

Alias for AsyncEach#if.

Parameters:
Name Type Description
predicate function
Returns:
Type
AsyncEach

(static) along(start, next) → {AsyncEach}

Creates a potentially infinite AsyncEach starting from an initial value, generating next values using a function.

Parameters:
Name Type Description
start *

Initial value

next function

Function (sync or async) to generate next value

Returns:

Infinite AsyncEach

Type
AsyncEach

(static) as(items) → {AsyncEach}

Coerces an input into an AsyncEach.

  • If already an AsyncEach, returns it.
  • If async iterable, wraps it.
  • If sync iterable, wraps it.
  • Otherwise, wraps scalar as one-item sequence.
Parameters:
Name Type Description
items *

Input sequence or value

Returns:
Type
AsyncEach

(static) each(…aaa) → {What}

Cartesian product of multiple iterables. Mirrors Each.each.

Parameters:
Name Type Attributes Description
aaa Iterable | AsyncIterable | Array.<Promise> <repeatable>
Returns:
Type
What

(static) else(aaa) → {AsyncEach}

Flattens an iterable of iterables into an AsyncEach.

Parameters:
Name Type Description
aaa Iterable | AsyncIterable

Iterable of iterables

Returns:

Flattened AsyncEach

Type
AsyncEach

(async, static) equal(aa, bb) → {Promise.<boolean>}

Checks deep equality between two async iterables or iterables.

Parameters:
Name Type Description
aa Iterable | AsyncIterable
bb Iterable | AsyncIterable
Returns:

Resolves to true if equal

Type
Promise.<boolean>

(static) match(…aaa) → {AsyncEach.<Array.<any>>}

Zips multiple iterables together.

Parameters:
Name Type Attributes Description
aaa Iterable | AsyncIterable | Array.<Promise> <repeatable>
Returns:
Type
AsyncEach.<Array.<any>>

(static) of(…items) → {AsyncEach}

Creates an AsyncEach from given items (values or Promises).

Parameters:
Name Type Attributes Description
items any <repeatable>

Items or promises

Returns:

A new AsyncEach instance

Type
AsyncEach

(static) self(aa) → {AsyncEach}

Yields the same iterable indefinitely.

Parameters:
Name Type Description
aa Iterable | AsyncIterable | Array.<Promise>
Returns:
Type
AsyncEach

(async, static) what(aa, opopt, gotopt) → {Promise.<*>}

Reduces an async iterable.

  • With op: reduces like Array.reduce (async/sync).
  • Without op: yields the first resolved item.
Parameters:
Name Type Attributes Description
aa AsyncIterable | Iterable.<Promise>
op function <optional>
got * <optional>
Returns:
Type
Promise.<*>

(static) when(aa, popt, startopt, inclusiveopt) → {AsyncEach}

Slices an async iterable or resolves Promises.

  • No arguments: bridges async iterables of Promises to async iterables of resolved values.
  • Predicate function: slices items starting/ending where the predicate matches.
  • Number: slices by index.
Parameters:
Name Type Attributes Default Description
aa Iterable | AsyncIterable | Array.<Promise>

Input iterable (can contain Promises)

p function | number <optional>

Predicate or index for slicing

start boolean <optional>
true

Whether to start or end at the matching element

inclusive boolean <optional>
start

Include the matched element

Returns:

Returns an AsyncEach of resolved or sliced values

Type
AsyncEach