Skip to main content

AsyncIterable

Index

Accessor

size

  • size<Value>(self): Promise<Int>
  • Returns the size of iterable

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    await AsyncIterable.size(iterable); // 3

Constructor

create

  • create<Value>(iteratorFn): AsyncIterable<Value>
  • Iterable constructor

    @example
    const iterable = AsyncIterable.create(() => ({
    next() { ... }
    }))

empty

  • empty<Value>(): AsyncIterable<Value>
  • Returns an iterable that have no value

    @example
    Array.from(AsyncIterable.empty()) // == []

generate

  • generate<Value>(length, mapFn): AsyncIterable<Value>
  • Generate an iterable of length using mapFn(index) on each element

    @example
    AsyncIterable.generate(3, () => 'a');// == AsyncIterable.of('a', 'a', 'a')
    AsyncIterable.generate(3, (index) => index * 2);// == AsyncIterable.of(0, 2, 4)

of

  • of<Value>(...values): AsyncIterable<Value>
  • Create an iterable of given values

    @example
    AsyncIterable.of('a', 'b', 'c');// 'a', 'b', 'c'

Predicate

every

  • every<Value>(self, predicate): Promise<boolean>
  • Tests whether all elements in the async iterable pass the test implemented by the provided function.

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    await AsyncIterable.every(iterable, (currentValue) => currentValue >= 1); // true
    await AsyncIterable.every(iterable, (currentValue) => currentValue >= 2); // false
    await AsyncIterable.every(iterable, (currentValue) => currentValue < 0); // false

some

  • some<Value>(self, predicate): Promise<boolean>
  • Tests whether any element in the async iterable pass the test implemented by the provided function.

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    await AsyncIterable.some(iterable, (currentValue) => currentValue >= 1); // true
    await AsyncIterable.some(iterable, (currentValue) => currentValue >= 2); // true
    await AsyncIterable.some(iterable, (currentValue) => currentValue < 0); // false

Type

hasInstance

  • hasInstance(anyValue): anyValue is AsyncIterable<unknown, any, any>
  • Returns true if anyValue is a valid AsyncIterable

    @example
    AsyncIterable.hasInstance({});// false
    AsyncIterable.hasInstance([]);// true
    AsyncIterable.hasInstance({ [Symbol.iterator]: () => ({ next: () => ({ done: true }) }) });// true

Other

chunks

  • chunks<Value>(self, chunkSize): AsyncIterable<Value[]>
  • Splits an async iterable into chunks of a specified size

    @example
    const iterator = AsyncIterable.from([1, 2, 3, 4, 5]);
    const chunks = AsyncIterable.chunks(iterator, 2); // == AsyncIterable.of([1, 2], [3, 4], [5])

concat

  • concat<Value>(iterables): AsyncIterable<Value>
  • Concatenates multiple iterables into a single iterable

    @example
    const iterable = AsyncIterable.of(
    AsyncIterable.of(1, 2),
    AsyncIterable.of(3, 4),
    );
    AsyncIterable.concat(iterable); // == AsyncIterable.of(1, 2, 3, 4)

drop

  • drop<Value>(self, limit): AsyncIterable<Value>
  • Skips the given number of elements at the start of this iterator.

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    AsyncIterable.drop(iterable, 1); // == AsyncIterable.of(2, 3)

filter

  • filter<Value>(self, predicate): AsyncIterable<Value>
  • Return a new iterator that filters values using predicate

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    AsyncIterable.filter(
    iterable,
    (currentValue, currentIndex) => currentValue > 1,
    );// == AsyncIterable.of(2, 3)

find

  • find<Value>(self, predicate): Promise<Option<Value>>
  • Returns the first element for which the given function returns true, otherwise undefined.

    @example
    const iterable = AsyncIterable.of(
    { name: 'amy', id: 1 },
    { name: 'bob', id: 2 }
    );
    await AsyncIterable.find(iterable, (item) => item.name === 'bob') // { name: 'bob', id: 2 }
    await AsyncIterable.find(iterable, (item) => item.name === 'cat') // undefined

flatMap

  • flatMap<ValueFrom, ValueTo>(self, mapFn): AsyncIterable<ValueTo>
  • Return a new Iterable which applies mapFn and concatenate the result to the previous

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    await AsyncIterable.flatMap(
    iterable,
    (currentValue, currentIndex) => AsyncIterable.of(currentValue, currentValue * 2),
    );// == AsyncIterable.of(1, 2, 2, 4, 3, 6)

map

  • map<ValueFrom, ValueTo>(self, mapFn): AsyncIterable<ValueTo>
  • Return a new Iterable which applies mapFn to each values

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    AsyncIterable.map(
    iterable,
    async (currentValue, currentIndex) => currentValue * 2,
    );// == AsyncIterable.of(2, 4, 6)

reduce

  • reduce<Value, Return>(self, reducer, initialValue): Promise<Return>
  • Reduce an initialValue to the reducer function

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    await AsyncIterable.reduce(
    iterable,
    (total, value) => total + value,
    0,
    );// 6

take

  • take<Value>(self, limit): AsyncIterable<Value>
  • Take a specified number of elements from an iterable.

    @example
    const iterable = AsyncIterable.of(1, 2, 3);
    AsyncIterable.take(iterable, 2); // == AsyncIterable.of(1, 2)

zip

  • zip<L, R>(left, right): AsyncIterable<[L, R]>
  • Combine two iterables into an iterable of couple of their values. The result has the size of the smallest iterable used.

    @example
    const left = AsyncIterable.of(1, 2, 3);
    const right = AsyncIterable.of('a', 'b');
    AsyncIterable.zip(left, right);// == AsyncIterable.of([1, 'a'], [2, 'b'])