Options
All
  • Public
  • Public/Protected
  • All
Menu

Module async

Index

Variables

Variables

Const AsyncIterable

AsyncIterable: { asIndexedPairs: <Item>(iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<[number, Item]>; drop: <Item>(limit: number, iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<Item>; every: <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => Promise<boolean>; filter: <Item>(filterFn: (x: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<Item>; find: <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => Promise<undefined | Item>; flatMap: <In, Out>(mapperFn: (x: In) => Out | Out[], iterable: AsyncOrSyncIterable<In>) => AsyncIterable<Out>; forEach: <Item>(fn: (item: Item) => void, iterable: AsyncOrSyncIterable<Item>) => Promise<void>; fromIterable: <Item>(iterable: Iterable<Item>) => AsyncIterable<Item>; map: <In, Out>(mapperFn: (x: In) => Out, iterable: AsyncOrSyncIterable<In>) => AsyncIterable<Out>; reduce: { <In, Out>(reducer: (acc: Out, item: In) => Out, initialValue: Out, iterable: AsyncOrSyncIterable<In>): Promise<Out>; <InOut>(reducer: (acc: InOut, item: InOut) => InOut, iterable: AsyncOrSyncIterable<InOut>): Promise<InOut> }; some: <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => Promise<boolean>; take: <Item>(limit: number, iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<Item>; toArray: <Item>(iterable: AsyncOrSyncIterable<Item>) => Promise<Item[]>; zip: { <MArr>(iterables: MArr): AsyncIterable<{[ Key in keyof MArr]: UnwrapAsyncOrSyncIterable<MArr[Key]> }>; <MObj>(iterables: MObj): AsyncIterable<{[ Key in keyof MObj]: UnwrapAsyncOrSyncIterable<MObj[Key]> }> } } = ...

Type declaration

  • asIndexedPairs: <Item>(iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<[number, Item]>
      • <Item>(iterable: AsyncOrSyncIterable<Item>): AsyncIterable<[number, Item]>
      • Each item x of iterable is converted to an item [index, x] in the returned iterable.

        While Array methods such as .map() and .filter() always provide indices, the functions in this module don’t. Therefore, this function is useful whenever we need indices – e.g., to determine which item is first in an iterable.

        assert.deepEqual(
          toArray(Iterable.asIndexedPairs(['a', 'b', 'c'])),
          [[0, 'a'], [1, 'b'], [2, 'c']]
        );
        

        Type parameters

        • Item

        Parameters

        • iterable: AsyncOrSyncIterable<Item>

        Returns AsyncIterable<[number, Item]>

  • drop: <Item>(limit: number, iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<Item>
      • <Item>(limit: number, iterable: AsyncOrSyncIterable<Item>): AsyncIterable<Item>
      • Returns an iterable with all items of iterable, except for the first limit ones.

        assert.deepEqual(
          toArray(Iterable.drop(1, ['a', 'b', 'c'])),
          ['b', 'c']
        );
        

        Type parameters

        • Item

        Parameters

        • limit: number
        • iterable: AsyncOrSyncIterable<Item>

        Returns AsyncIterable<Item>

  • every: <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => Promise<boolean>
      • <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>): Promise<boolean>
      • Iterates over each item of iterable and returns false as soon as pred(item) returns a falsy value. It short-circuits in that case and doesn’t visit the remaining items of iterable. That means, we can use this function with iterables of infinite length.

        This function returns true if pred() never returns a falsy value and iterable is of finite length.

        function* naturalNumbers() {
          for(let i=0;; i++) {
            yield i;
          }
        }
        assert.equal(
          Iterable.every((item) => item > 0, [5, -3, 12]),
          false
        );
        assert.equal(
          Iterable.every((item) => item < 0, [5, -3, 12]),
          false
        );
        assert.equal(
          Iterable.every((item) => item >= -3, [5, -3, 12]),
          true
        );
        
        assert.equal(
          Iterable.every((item) => item <= 10, naturalNumbers()),
          false
        );
        

        Type parameters

        • Item

        Parameters

        • pred: (item: Item) => boolean
            • (item: Item): boolean
            • Parameters

              • item: Item

              Returns boolean

        • iterable: AsyncOrSyncIterable<Item>

        Returns Promise<boolean>

  • filter: <Item>(filterFn: (x: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<Item>
      • <Item>(filterFn: (x: Item) => boolean, iterable: AsyncOrSyncIterable<Item>): AsyncIterable<Item>
      • This function returns an iterable that contains only those items of iterable for which filterFn returns a truthy value.

        assert.deepEqual(
          toArray(Iterable.filter(x => x < 0, [-1, 3, -4, 8])),
          [-1, -4]
        );
        

        Type parameters

        • Item

        Parameters

        • filterFn: (x: Item) => boolean
            • (x: Item): boolean
            • Parameters

              • x: Item

              Returns boolean

        • iterable: AsyncOrSyncIterable<Item>

        Returns AsyncIterable<Item>

  • find: <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => Promise<undefined | Item>
      • <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>): Promise<undefined | Item>
      • Iterates over each item of iterable and returns item as soon as pred(item) returns a truthy value. It short-circuits in that case and doesn’t visit the remaining items of iterable. That means, we can use this function with iterables of infinite length.

        This function returns undefined if pred() never returns a truthy value and iterable is of finite length.

        function* naturalNumbers() {
          for(let i=0;; i++) {
            yield i;
          }
        }
        assert.equal(
          Iterable.find((item) => item > 0, [5, -3, 12, -8]),
          5
        );
        assert.equal(
          Iterable.find((item) => item < 0, [5, -3, 12, -8]),
          -3
        );
        
        assert.equal(
          Iterable.find((item) => item > 20, naturalNumbers()),
          21
        );
        

        Type parameters

        • Item

        Parameters

        • pred: (item: Item) => boolean
            • (item: Item): boolean
            • Parameters

              • item: Item

              Returns boolean

        • iterable: AsyncOrSyncIterable<Item>

        Returns Promise<undefined | Item>

  • flatMap: <In, Out>(mapperFn: (x: In) => Out | Out[], iterable: AsyncOrSyncIterable<In>) => AsyncIterable<Out>
      • <In, Out>(mapperFn: (x: In) => Out | Out[], iterable: AsyncOrSyncIterable<In>): AsyncIterable<Out>
      • Each item of iterable is converted to zero or more items in the returned iterable, depending on whether mapperFn returns a single value or an Array (and on how long that Array is). This enables us to:

        • Filter and map at the same time. We omit by returning [] and map by returning [result].
        • Expand single values into multiple values (see below).
        assert.deepEqual(
          toArray(Iterable.flatMap(x => x, ['a', 'b', 'c'])),
          ['a', 'b', 'c']
        );
        assert.deepEqual(
          toArray(Iterable.flatMap(x => [], ['a', 'b', 'c'])),
          []
        );
        assert.deepEqual(
          toArray(Iterable.flatMap(x => [x], ['a', 'b', 'c'])),
          ['a', 'b', 'c']
        );
        assert.deepEqual(
          toArray(Iterable.flatMap(x => [x, x], ['a', 'b', 'c'])),
          ['a', 'a', 'b', 'b', 'c', 'c']
        );
        

        Type parameters

        • In

        • Out

        Parameters

        • mapperFn: (x: In) => Out | Out[]
            • (x: In): Out | Out[]
            • Parameters

              • x: In

              Returns Out | Out[]

        • iterable: AsyncOrSyncIterable<In>

        Returns AsyncIterable<Out>

  • forEach: <Item>(fn: (item: Item) => void, iterable: AsyncOrSyncIterable<Item>) => Promise<void>
      • <Item>(fn: (item: Item) => void, iterable: AsyncOrSyncIterable<Item>): Promise<void>
      • Retrieves each item of iterable and invokes fn() with it.

        const result = [];
        Iterable.forEach(x => result.push(x + x), ['a', 'b', 'c'])
        assert.deepEqual(
          result,
          ['aa', 'bb', 'cc']
        );
        

        Type parameters

        • Item

        Parameters

        • fn: (item: Item) => void
            • (item: Item): void
            • Parameters

              • item: Item

              Returns void

        • iterable: AsyncOrSyncIterable<Item>

        Returns Promise<void>

  • fromIterable: <Item>(iterable: Iterable<Item>) => AsyncIterable<Item>
      • <Item>(iterable: Iterable<Item>): AsyncIterable<Item>
      • Type parameters

        • Item

        Parameters

        • iterable: Iterable<Item>

        Returns AsyncIterable<Item>

  • map: <In, Out>(mapperFn: (x: In) => Out, iterable: AsyncOrSyncIterable<In>) => AsyncIterable<Out>
      • <In, Out>(mapperFn: (x: In) => Out, iterable: AsyncOrSyncIterable<In>): AsyncIterable<Out>
      • This function returns an iterable where each item was produced by applying mapperFn to an item of iterable.

        assert.deepEqual(
          toArray(Iterable.map(x => x + x, ['a', 'b', 'c'])),
          ['aa', 'bb', 'cc']
        );
        

        Type parameters

        • In

        • Out

        Parameters

        • mapperFn: (x: In) => Out
            • (x: In): Out
            • Parameters

              • x: In

              Returns Out

        • iterable: AsyncOrSyncIterable<In>

        Returns AsyncIterable<Out>

  • reduce: { <In, Out>(reducer: (acc: Out, item: In) => Out, initialValue: Out, iterable: AsyncOrSyncIterable<In>): Promise<Out>; <InOut>(reducer: (acc: InOut, item: InOut) => InOut, iterable: AsyncOrSyncIterable<InOut>): Promise<InOut> }
      • <In, Out>(reducer: (acc: Out, item: In) => Out, initialValue: Out, iterable: AsyncOrSyncIterable<In>): Promise<Out>
      • <InOut>(reducer: (acc: InOut, item: InOut) => InOut, iterable: AsyncOrSyncIterable<InOut>): Promise<InOut>
      • Feeds all items of iterable to reducer() which folds them into acc. The last acc returned by reducer() is the result of this function.

        assert.deepEqual(
          Iterable.reduce((acc, item) => acc + item, ['a', 'b', 'c']),
          'abc'
        );
        assert.deepEqual(
          Iterable.reduce((acc, item) => acc + item, 'x', ['a', 'b', 'c']),
          'xabc'
        );
        
        throws

        TypeError If iterable is empty and no initialValue is provided.

        Type parameters

        • In

        • Out

        Parameters

        • reducer: (acc: Out, item: In) => Out
            • (acc: Out, item: In): Out
            • Parameters

              • acc: Out
              • item: In

              Returns Out

        • initialValue: Out

          The first value of acc. If it is missing, the first item of iterable is used, instead.

        • iterable: AsyncOrSyncIterable<In>

        Returns Promise<Out>

      • Type parameters

        • InOut

        Parameters

        • reducer: (acc: InOut, item: InOut) => InOut
            • (acc: InOut, item: InOut): InOut
            • Parameters

              • acc: InOut
              • item: InOut

              Returns InOut

        • iterable: AsyncOrSyncIterable<InOut>

        Returns Promise<InOut>

  • some: <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>) => Promise<boolean>
      • <Item>(pred: (item: Item) => boolean, iterable: AsyncOrSyncIterable<Item>): Promise<boolean>
      • Iterates over each item of iterable and returns true as soon as pred(item) returns a truthy value. It short-circuits in that case and doesn’t visit the remaining items of iterable. That means, we can use this function with iterables of infinite length.

        This function returns false if pred() never returns a truthy value and iterable is of finite length.

        function* naturalNumbers() {
          for(let i=0;; i++) {
            yield i;
          }
        }
        assert.equal(
          await AsyncIterable.some((item) => item > 0, fi([5, -3, 12])),
          true
        );
        assert.equal(
          await AsyncIterable.some((item) => item < 0, fi([5, -3, 12])),
          true
        );
        assert.equal(
          await AsyncIterable.some((item) => item < -3, fi([5, -3, 12])),
          false
        );
        
        assert.equal(
          await AsyncIterable.some((item) => item > 10, fi(naturalNumbers())),
          true
        );
        

        Type parameters

        • Item

        Parameters

        • pred: (item: Item) => boolean
            • (item: Item): boolean
            • Parameters

              • item: Item

              Returns boolean

        • iterable: AsyncOrSyncIterable<Item>

        Returns Promise<boolean>

  • take: <Item>(limit: number, iterable: AsyncOrSyncIterable<Item>) => AsyncIterable<Item>
      • <Item>(limit: number, iterable: AsyncOrSyncIterable<Item>): AsyncIterable<Item>
      • Returns an iterable with the first limit items of iterable.

        function* naturalNumbers() {
          for(let i=0;; i++) {
            yield i;
          }
        }
        assert.deepEqual(
          toArray(Iterable.take(3, naturalNumbers())),
          [0, 1, 2]
        );
        assert.deepEqual(
          toArray(Iterable.take(2, ['a', 'b', 'c'])),
          ['a', 'b']
        );
        

        Type parameters

        • Item

        Parameters

        • limit: number
        • iterable: AsyncOrSyncIterable<Item>

        Returns AsyncIterable<Item>

  • toArray: <Item>(iterable: AsyncOrSyncIterable<Item>) => Promise<Item[]>
      • <Item>(iterable: AsyncOrSyncIterable<Item>): Promise<Item[]>
      • Type parameters

        • Item

        Parameters

        • iterable: AsyncOrSyncIterable<Item>

        Returns Promise<Item[]>

  • zip: { <MArr>(iterables: MArr): AsyncIterable<{[ Key in keyof MArr]: UnwrapAsyncOrSyncIterable<MArr[Key]> }>; <MObj>(iterables: MObj): AsyncIterable<{[ Key in keyof MObj]: UnwrapAsyncOrSyncIterable<MObj[Key]> }> }
      • <MArr>(iterables: MArr): AsyncIterable<{[ Key in keyof MArr]: UnwrapAsyncOrSyncIterable<MArr[Key]> }>
      • <MObj>(iterables: MObj): AsyncIterable<{[ Key in keyof MObj]: UnwrapAsyncOrSyncIterable<MObj[Key]> }>
      • Depending on its argument, this function converts:

        • An n-ary Array of iterables into an iterable of n-ary Arrays whose elements are the items of those iterables.
        • An object whose property values are iterables into an iterable of objects whose property values are the items of those iterables.
        assert.deepEqual(
          toArray(Iterable.zip({first: ['a', 'b'], second: [0, 1, 2] })),
          [ {first: 'a', second: 0}, {first: 'b', second: 1} ]
        );
        assert.deepEqual(
          toArray(Iterable.zip([ ['a', 'b'], [0, 1, 2] ])),
          [ ['a', 0], ['b', 1] ]
        );
        

        Type parameters

        • MArr: MixedArray<AsyncOrSyncIterable<unknown>>

        Parameters

        • iterables: MArr

        Returns AsyncIterable<{[ Key in keyof MArr]: UnwrapAsyncOrSyncIterable<MArr[Key]> }>

      • Type parameters

        • MObj: MixedObject<AsyncOrSyncIterable<unknown>>

        Parameters

        • iterables: MObj

        Returns AsyncIterable<{[ Key in keyof MObj]: UnwrapAsyncOrSyncIterable<MObj[Key]> }>

Generated using TypeDoc