Skip to main content

AsyncIterable

Index

Accessor

size

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

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

    Type parameters

    • Value

Constructor

create

  • create<Value>(iteratorFn: () => AsyncIterator<Value, any, any>): AsyncIterable<Value>
  • Iterable constructor

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

    Type parameters

    • Value

empty

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

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

    Type parameters

    • Value = never

generate

  • generate<Value>(length: number, mapFn: (index: Int) => Awaitable<Value>): 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)

    Type parameters

    • Value

of

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

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

    Type parameters

    • Value

Predicate

every

  • every<Value>(self: AsyncIterableLike<Value>, predicate: (currentValue: Value, currentIndex: Int) => Awaitable<boolean>): 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

    Type parameters

    • Value

some

  • some<Value>(self: AsyncIterableLike<Value>, predicate: (currentValue: Value, currentIndex: Int) => Awaitable<boolean>): 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 parameters

    • Value

Type

hasInstance

  • hasInstance(anyValue: unknown): 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

filter

  • filter<Value>(self: AsyncIterableLike<Value>, predicate: (currentValue: Value, currentIndex: Int) => Awaitable<boolean>): 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)

    Type parameters

    • Value

find

  • find<Value>(self: AsyncIterableLike<Value>, predicate: (currentValue: Value, currentIndex: number) => Awaitable<boolean>): 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

    Type parameters

    • Value

flatMap

  • flatMap<ValueFrom, ValueTo>(self: AsyncIterableLike<ValueFrom>, mapFn: (currentValue: ValueFrom, currentIndex: Int) => AsyncIterableLike<ValueTo>): 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)

    Type parameters

    • ValueFrom
    • ValueTo

map

  • map<ValueFrom, ValueTo>(self: AsyncIterableLike<ValueFrom>, mapFn: (currentValue: ValueFrom, currentIndex: Int) => Awaitable<ValueTo>): 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)

    Type parameters

    • ValueFrom
    • ValueTo

reduce

  • reduce<Value, Return>(self: AsyncIterableLike<Value>, reducer: (accumulator: Return, currentValue: Value, currentIndex: Int) => Awaitable<Return>, initialValue: Return): 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

    Type parameters

    • Value
    • Return

zip

  • zip<L, R>(left: AsyncIterableLike<L>, right: AsyncIterableLike<R>): 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'])

    Type parameters

    • L
    • R