AsyncIterator helpers
March 13, 2023About 2 min
AsyncIterator helpers
Modules
esnext.async-iterator.constructoresnext.async-iterator.dropesnext.async-iterator.everyesnext.async-iterator.filteresnext.async-iterator.findesnext.async-iterator.flat-mapesnext.async-iterator.for-eachesnext.async-iterator.fromesnext.async-iterator.indexedesnext.async-iterator.mapesnext.async-iterator.reduceesnext.async-iterator.someesnext.async-iterator.takeesnext.async-iterator.to-arrayesnext.iterator.to-async
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
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
pureversion, new%AsyncIteratorPrototype%methods are not added to the real%AsyncIteratorPrototype%, they available only on wrappers - instead of[].values().toAsync().map(fn)useAsyncIterator.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 useFunctionconstructor. However, that breaks compatibility with CSP. So, if you wanna use the real%AsyncIteratorPrototype%, you should setUSE_FUNCTION_CONSTRUCTORoption in thecore-js/configuratortotrue:
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/configuratoran 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