Skip to main content

AsyncIterator helpers

About 2 minfeaturees-proposal

AsyncIterator helpersopen in new window

Modules

types

class Iterator<T> {
  toAsync(): AsyncIterator<T>;
}

class AsyncIterator<T> {
  static from<U>(
    iterable: AsyncIterable<U> | Iterable<U> | AsyncIterator<U>
  ): AsyncIterator<U>;
  drop(limit: number): AsyncIterator<T>;
  every(
    callbackfn: (value: T, couner: number) => Promise<boolean>
  ): Promise<boolean>;
  filter(
    callbackfn: (value: T, couner: number) => Promise<boolean>
  ): AsyncIterator<T>;
  find(callbackfn: (value: T, couner: number) => Promise<boolean>): Promise<T>;
  flatMap<U>(
    callbackfn: (
      value: T,
      couner: number
    ) => AsyncIterable<U> | Iterable<U> | AsyncIterator<U>
  ): AsyncIterator<U>;
  forEach(
    callbackfn: (value: T, couner: number) => Promise<void>
  ): Promise<void>;
  map<U>(
    callbackfn: (value: T, couner: number) => Promise<U>
  ): AsyncIterator<U>;
  reduce<U>(
    callbackfn: (memo: U, value: T, couner: Promise<number>) => U,
    initialValue: U
  ): Promise<U>;
  some(callbackfn: (value: T, couner: number) => boolean): Promise<boolean>;
  take(limit: number): AsyncIterator<T>;
  toArray(): Promise<Array<T>>;
  [Symbol.toStringTag]: "AsyncIterator";
}

Entry points

core-js/proposals/async-iterator-helpers
core-js(-pure)/actual|full/async-iterator
core-js(-pure)/actual|full/async-iterator/drop
core-js(-pure)/actual|full/async-iterator/every
core-js(-pure)/actual|full/async-iterator/filter
core-js(-pure)/actual|full/async-iterator/find
core-js(-pure)/actual|full/async-iterator/flat-map
core-js(-pure)/actual|full/async-iterator/for-each
core-js(-pure)/actual|full/async-iterator/from
core-js(-pure)/actual|full/async-iterator/indexed
core-js(-pure)/actual|full/async-iterator/map
core-js(-pure)/actual|full/async-iterator/reduce
core-js(-pure)/actual|full/async-iterator/some
core-js(-pure)/actual|full/async-iterator/take
core-js(-pure)/actual|full/async-iterator/to-array

Example

Exampleopen in new window

await AsyncIterator.from([1, 2, 3, 4, 5, 6, 7])
  .drop(1)
  .take(5)
  .filter((it) => it % 2)
  .map((it) => it ** 2)
  .toArray(); // => [9, 25]
await [1, 2, 3]
  .values()
  .toAsync()
  .map(async (it) => it ** 2)
  .toArray(); // => [1, 4, 9]

Caveats

  • For preventing prototypes pollution, in the pure version, new %AsyncIteratorPrototype% methods are not added to the real %AsyncIteratorPrototype%, they available only on wrappers - instead of [].values().toAsync().map(fn) use AsyncIterator.from([]).map(fn).
  • Now, we have access to the real %AsyncIteratorPrototype% only with usage async generators syntax. So, for compatibility the library with old browsers, we should use Function constructor. However, that breaks compatibility with CSP. So, if you wanna use the real %AsyncIteratorPrototype%, you should set USE_FUNCTION_CONSTRUCTOR option in the core-js/configurator to true:
const configurator = require("core-js/configurator");

configurator({ USE_FUNCTION_CONSTRUCTOR: true });

require("core-js/actual/async-iterator");

(async function* () {
  /* empty */
})() instanceof AsyncIterator; // => true
  • As an alternative, you could pass to the core-js/configurator an object that will be considered as %AsyncIteratorPrototype%:
const configurator = require("core-js/configurator");
const { getPrototypeOf } = Object;

configurator({
  AsyncIteratorPrototype: getPrototypeOf(
    getPrototypeOf(
      getPrototypeOf(
        (async function* () {
          /* empty */
        })()
      )
    )
  ),
});

require("core-js/actual/async-iterator");

(async function* () {
  /* empty */
})() instanceof AsyncIterator; // => true