AsyncIterable
Index
Accessor
size
- const iterable = AsyncIterable.of(1, 2, 3);await AsyncIterable.size(iterable); // 3
Constructor
create
Iterable constructor
const iterable = AsyncIterable.create(() => ({next() { ... }}))
empty
Returns an iterable that have no value
Array.from(AsyncIterable.empty()) // == []
generate
Generate an iterable of
lengthusingmapFn(index)on each elementAsyncIterable.generate(3, () => 'a');// == AsyncIterable.of('a', 'a', 'a')AsyncIterable.generate(3, (index) => index * 2);// == AsyncIterable.of(0, 2, 4)
of
Create an iterable of given
valuesAsyncIterable.of('a', 'b', 'c');// 'a', 'b', 'c'
Predicate
every
Tests whether all elements in the async iterable pass the test implemented by the provided function.
const iterable = AsyncIterable.of(1, 2, 3);await AsyncIterable.every(iterable, (currentValue) => currentValue >= 1); // trueawait AsyncIterable.every(iterable, (currentValue) => currentValue >= 2); // falseawait AsyncIterable.every(iterable, (currentValue) => currentValue < 0); // false
some
Tests whether any element in the async iterable pass the test implemented by the provided function.
const iterable = AsyncIterable.of(1, 2, 3);await AsyncIterable.some(iterable, (currentValue) => currentValue >= 1); // trueawait AsyncIterable.some(iterable, (currentValue) => currentValue >= 2); // trueawait AsyncIterable.some(iterable, (currentValue) => currentValue < 0); // false
Type
hasInstance
Returns
trueifanyValueis a valid AsyncIterableAsyncIterable.hasInstance({});// falseAsyncIterable.hasInstance([]);// trueAsyncIterable.hasInstance({ [Symbol.iterator]: () => ({ next: () => ({ done: true }) }) });// true
Other
chunks
Splits an async iterable into chunks of a specified size
const iterator = AsyncIterable.from([1, 2, 3, 4, 5]);const chunks = AsyncIterable.chunks(iterator, 2); // == AsyncIterable.of([1, 2], [3, 4], [5])
concat
Concatenates multiple iterables into a single iterable
const iterable = AsyncIterable.of(AsyncIterable.of(1, 2),AsyncIterable.of(3, 4),);AsyncIterable.concat(iterable); // == AsyncIterable.of(1, 2, 3, 4)
drop
Skips the given number of elements at the start of this iterator.
const iterable = AsyncIterable.of(1, 2, 3);AsyncIterable.drop(iterable, 1); // == AsyncIterable.of(2, 3)
filter
Return a new iterator that filters values using
predicateconst iterable = AsyncIterable.of(1, 2, 3);AsyncIterable.filter(iterable,(currentValue, currentIndex) => currentValue > 1,);// == AsyncIterable.of(2, 3)
find
Returns the first element for which the given function returns true, otherwise undefined.
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
Return a new Iterable which applies
mapFnand concatenate the result to the previousconst 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
Return a new Iterable which applies
mapFnto each valuesconst iterable = AsyncIterable.of(1, 2, 3);AsyncIterable.map(iterable,async (currentValue, currentIndex) => currentValue * 2,);// == AsyncIterable.of(2, 4, 6)
reduce
Reduce an
initialValueto thereducerfunctionconst iterable = AsyncIterable.of(1, 2, 3);await AsyncIterable.reduce(iterable,(total, value) => total + value,0,);// 6
take
Take a specified number of elements from an iterable.
const iterable = AsyncIterable.of(1, 2, 3);AsyncIterable.take(iterable, 2); // == AsyncIterable.of(1, 2)
zip
Combine two iterables into an iterable of couple of their values. The result has the size of the smallest iterable used.
const left = AsyncIterable.of(1, 2, 3);const right = AsyncIterable.of('a', 'b');AsyncIterable.zip(left, right);// == AsyncIterable.of([1, 'a'], [2, 'b'])
Returns the size of iterable