Asynchronous.Iterator_Cascade_Callbacks
Asynchronous Iterator that chains asynchronous iterables and/or callback function execution
Author
S0AndS0
License
AGPL-3.0
See
| Name | Type |
|---|---|
Initial_Iterable_Value |
unknown |
- #noOpCallback
- [asyncIterator]
- collect
- collectToArray
- collectToFunction
- collectToObject
- copyCallbacksOnto
- entries
- filter
- forEach
- inspect
- limit
- map
- next
- popCallbackObject
- popCallbackWrapper
- pushCallbackWrapper
- skip
- step
- take
- zip
• new Iterator_Cascade_Callbacks<Initial_Iterable_Value>(iterable)
Instantiates new instance of Iterator_Cascade_Callbacks from iterable input
| Name | Type |
|---|---|
Initial_Iterable_Value |
unknown |
| Name | Type | Description |
|---|---|---|
iterable |
unknown |
Currently may be an array, object, generator, iterator, or object that implements Symbol.asyncIterator type |
Throws
when passed unsupported iterables
src/asynchronous/iterator-cascade-callbacks.ts:84
• Private #noOpParameters: never[] = []
Cheep reusable array reference satisfies TypeScript, and pushCallbackWrapper, requirements
src/asynchronous/iterator-cascade-callbacks.ts:1025
• callbacks: Callback_Object<Initial_Iterable_Value, unknown, unknown[], unknown>[]
Array of objects that hold pointers to Asynchronous.Callback_Wrapper and Asynchronous.Callback_Function
See
src/asynchronous/iterator-cascade-callbacks.ts:42
• done: boolean
Report when/if instance of Asynchronous.Iterator_Cascade_Callbacks is done producing output via this.next()
Notes
- The
.doneproperty is used byfor (value of iterable)loops to trigger termination
src/asynchronous/iterator-cascade-callbacks.ts:50
• iterator: IterableIterator<Yielded_Data<Initial_Iterable_Value, unknown>> | AsyncGenerator<Yielded_Data<Initial_Iterable_Value, unknown>, void, unknown>
Generator that this.next() cascades values through this.callbacks list
See
- For how this property is assigned
- For how iterator values are passed through
this.callbackslist - For when Array runtime type is detected
- For when Generator runtime type is detected
- For when AsyncGenerator or AsyncIterator runtime type is detected
- for when Object runtime type is detected
src/asynchronous/iterator-cascade-callbacks.ts:62
• state: Object
Allow for finer-grain states than this.done
See
| Name | Type |
|---|---|
paused |
boolean |
resumed |
boolean |
src/asynchronous/iterator-cascade-callbacks.ts:70
• yielded_data: Yielded_Data<Initial_Iterable_Value, unknown>
src/asynchronous/iterator-cascade-callbacks.ts:75
• get value(): Initial_Iterable_Value
Get data from this.yielded_data.content
Initial_Iterable_Value
Notes
- Implicitly called by
for (value of iterable)loops
src/asynchronous/iterator-cascade-callbacks.ts:246
▸ Private #noOpCallback(): void
Cheep reusable function reference satisfies TypeScript, and pushCallbackWrapper, requirements
void
src/asynchronous/iterator-cascade-callbacks.ts:1020
▸ [asyncIterator](): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
See
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols
src/asynchronous/iterator-cascade-callbacks.ts:1031
▸ collect<Target, Value, Callback_Or_Amount>(target, callback_or_amount?, amount?): Promise<Target>
Collects results from this to either an Array or Object
| Name | Type |
|---|---|
Target |
extends unknown = unknown |
Value |
Initial_Iterable_Value |
Callback_Or_Amount |
extends undefined | number | Collect_To_Function<Target, Value> = undefined |
| Name | Type | Description |
|---|---|---|
target |
Target |
When target is Array values are pushed, when target is Object key value pares are assigned, callback is required for other types |
callback_or_amount? |
Callback_Or_Amount |
Callback function for collecting to custom type, or number to limit collection to |
amount? |
number |
Limit collection to no more than amount |
Promise<Target>
Throws
Notes
- Order of detection for
callback_or_amountis;Function,Array,Object
See
- For when
Arrayinstance is detected - For when
Functioninstance is detected - For when
Objectinstance is detected
src/asynchronous/iterator-cascade-callbacks.ts:270
▸ collectToArray<Target>(target, amount?): Promise<Target>
Collects results from this.next() to an Array
| Name | Type |
|---|---|
Target |
extends unknown[] = unknown[] |
| Name | Type | Description |
|---|---|---|
target |
Target |
Array to push collected values to |
amount? |
number |
Limit collection to no more than amount |
Promise<Target>
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([5, 6, 7, 8, 9]);
(async () => {
const collection = await icca
.filter((value) => {
return value % 2 === 0;
})
.collectToArray([1, 2, 3]);
console.log(collection);
//> [ 1, 2, 3, 6, 8 ]
})();src/asynchronous/iterator-cascade-callbacks.ts:322
▸ collectToFunction<Target, Value, Callback>(target, callback, amount?): Promise<Target>
Collects results from this.next() to a callback function target
| Name | Type |
|---|---|
Target |
unknown |
Value |
unknown |
Callback |
extends Collect_To_Function<any, any> = Collect_To_Function<Target, Value> |
| Name | Type | Description |
|---|---|---|
target |
Target |
Any object or primitive, will be passed to callback function along with value and index_or_key |
callback |
Callback |
Custom callback function for collecting iterated values |
amount? |
number |
Limit collection to no more than amount |
Promise<Target>
target - The object that callback function has mutated
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks({ spam: 'flavored', canned: 'ham' });
const map = new Map();
(async () => {
const collection = await icca.collectToFunction(map, (target, value, index_or_key) => {
target.set(index_or_key, value);
});
console.log(collection);
//> Map(2) { 'spam' => 'flavored', 'canned' => 'ham' }
})();src/asynchronous/iterator-cascade-callbacks.ts:369
▸ collectToObject<Target>(target, amount?): Promise<Target>
Collects results from this.next() to an Object
| Name | Type |
|---|---|
Target |
extends Dictionary<unknown> |
| Name | Type | Description |
|---|---|---|
target |
Target |
Dictionary like object to assign key value pares to |
amount? |
number |
Limit collection to no more than amount |
Promise<Target>
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks({ spam: 'flavored', canned: 'ham' });
(async () => {
const collection = await icca.collectToObject({});
console.log(collection);
//> { spam: 'flavored', canned: 'ham' }
})();src/asynchronous/iterator-cascade-callbacks.ts:418
▸ copyCallbacksOnto<T>(iterable): Iterator_Cascade_Callbacks<T>
Returns new instance of Iterator_Cascade_Callbacks with copy of this.callbacks
| Name | Type |
|---|---|
T |
unknown |
| Name | Type | Description |
|---|---|---|
iterable |
unknown |
Any compatible iterable object, iterator, or generator |
Notes
- New instance will share references to callback wrapper functions
Example
const iterable_one = [1, 2, 3, 4, 5];
const iterable_two = [9, 8, 7, 6, 5];
const icca_one = new Asynchronous.Iterator_Cascade_Callbacks(iterable_one);
icca_one.filter((value) => {
return value % 2 === 0;
}).map((evens) => {
return evens / 2;
});
const icca_two = icc_one.copyCallbacksOnto(iterable_two);
(async () => {
console.log('Collection One ->', await icca_one.collect([]));
//> [ 1, 2 ]
console.log('Collection Two ->', await icca_two.collect([]));
//> [ 4, 3 ]
})();src/asynchronous/iterator-cascade-callbacks.ts:475
▸ entries<Value, Parameters, Key>(): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Sets this.value to Yielded_Entry which contains [this.yielded_data.index_or_key, this.yielded_data.content]
| Name | Type |
|---|---|
Value |
Initial_Iterable_Value |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([9, 8, 7, 6, 5]);
(async () => {
const collection = await icca
.entries()
.filter(([index, value]) => {
return (value - index) % 3 === 0;
})
.collect([]);
console.log(collection);
//> [ [ 0, 9 ], [ 3, 6 ] ]
})();src/asynchronous/iterator-cascade-callbacks.ts:520
▸ filter<Value, Parameters, Key>(callback, ...parameters): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Sets this.value if callback function returns truthy, else consumes this.iterator and recomputes value for callback to test
| Name | Type |
|---|---|
Value |
Initial_Iterable_Value |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
callback |
Callback_Function<Value, boolean, Parameters, Key> |
Function that determines truth of value and/or index_or_key for each iteration |
...parameters |
Parameters |
List of arguments that are passed to callback on each iteration |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([9, 8, 7, 6, 5]);
(async () => {
const collection = icca
.filter((value) => {
return value % 2 === 0;
})
.collect([]);
console.log(collection);
//> [ 8, 6 ]
})();src/asynchronous/iterator-cascade-callbacks.ts:564
▸ forEach<Value, Parameters, Key>(callback, ...parameters): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Executes callback for each iteration
| Name | Type |
|---|---|
Value |
Initial_Iterable_Value |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
callback |
Callback_Function<Value, void, Parameters, Key> |
Function that generally does not mutate value or index_or_key for Iterator_Cascade_Callbacks instance |
...parameters |
Parameters |
List of arguments that are passed to callback on each iteration |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Notes
- If mutation of
valueorindex_or_keyare desired thenmapis a better option - No protections are in place to prevent mutation of
valueorindex_or_keyObjects
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([9, 8, 7, 6, 5]);
(async () => {
const collection = await icca
.forEach((value) => {
console.log(value);
})
.collect([]);
console.log(collection);
//> [ 9, 8, 7, 6, 5 ]
})();src/asynchronous/iterator-cascade-callbacks.ts:609
▸ inspect<Value, Parameters, Key>(callback, ...parameters): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Useful for debugging and inspecting iteration state
| Name | Type |
|---|---|
Value |
unknown |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
callback |
Callback_Function<Value, void, Parameters, Key> |
Function that logs something about each iteration |
...parameters |
Parameters |
List of arguments that are passed to callback on each iteration |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
See
Example
function inspector(
value,
index_or_key,
{ callback_object, iterator_cascade_callbacks },
...parameters
) {
console.log('value ->', value);
console.log('index_or_key ->', index_or_key);
console.log('callback_object ->', callback_object);
console.log('iterator_cascade_callbacks ->', iterator_cascade_callbacks);
}
const icca = new Asynchronous.Iterator_Cascade_Callbacks([9, 8, 7, 6, 5]);
(async () => {
const collection = await icca
.filter((value) => {
return value % 2 === 0;
})
.inspect(inspector)
.map((even) => {
return even / 2;
})
.inspect(inspector)
.collect([]);
})();src/asynchronous/iterator-cascade-callbacks.ts:667
▸ limit<Value, Parameters, Key>(amount): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Stops iteration when limit is reached
| Name | Type |
|---|---|
Value |
unknown |
Parameters |
extends unknown[] = [number, ...unknown[]] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
amount |
number |
Max number of values to compute |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Throws
Notes
- Useful when iterating over data of indeterminate, or infinite, length
- More predictable if ordered at the end of
this.callbackslist - Callbacks exist when
amountis reached will be called prior to throwingStop_Iteration
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([1, 2, 3, 4]);
(async () => {
const collection = await icca.limit(2).collect([]);
console.log(collection);
//> [1, 2]
})();src/asynchronous/iterator-cascade-callbacks.ts:711
▸ map<Value, Result, Parameters, Key>(callback, ...parameters): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Applies callback to modify value and/or index_or_key for each iteration
| Name | Type |
|---|---|
Value |
Initial_Iterable_Value |
Result |
Initial_Iterable_Value |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
callback |
Callback_Function<Value, Result, Parameters, Key> |
Function may modify value and/or index_or_key |
...parameters |
Parameters |
List of arguments that are passed to callback on each iteration |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Notes
- If callback does not return
Yielded_Data(array), then results from callback are used asvalueand initialindex_or_keyis reused
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([9, 8, 7, 6, 5]);
(async () => {
const collection = await icca
.filter((value) => {
return value % 2 === 0;
})
.map((value) => {
return value / 2;
})
.collect([]);
console.log(collection);
//> [4, 3]
})();src/asynchronous/iterator-cascade-callbacks.ts:761
▸ next(): Promise<Iterator_Cascade_Callbacks<Initial_Iterable_Value>>
Updates this.yielded_data.content from chaining this.callbacks list, and this.done from this.iterator.next()
Promise<Iterator_Cascade_Callbacks<Initial_Iterable_Value>>
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([1, 2, 3, 4]);
(async () => {
for await (let value of icca) {
console.log('value ->', value);
}
//> value -> 1
//> value -> 2
//> value -> 3
//> value -> 4
})();src/asynchronous/iterator-cascade-callbacks.ts:801
▸ popCallbackObject(): undefined | Callback_Object<unknown, unknown, unknown[], unknown>
Returns, and removes, last Callback_Object from this.callbacks list
undefined | Callback_Object<unknown, unknown, unknown[], unknown>
src/asynchronous/iterator-cascade-callbacks.ts:847
▸ popCallbackWrapper(): undefined | Callback_Wrapper<unknown, unknown, unknown[], unknown>
Removes last Callback_Object from this.callbacks list and returns Callback_Wrapper function
undefined | Callback_Wrapper<unknown, unknown, unknown[], unknown>
src/asynchronous/iterator-cascade-callbacks.ts:858
▸ pushCallbackWrapper<Value, Result, Parameters, Key>(options): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Instantiates Callback_Object with callback_wrapper and pushes to this.callbacks via this.pushCallbackObject
| Name | Type |
|---|---|
Value |
unknown |
Result |
unknown |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
options |
Object |
|
options.callback |
Callback_Function<Value, Result, Parameters, Key> |
- |
options.name |
string |
Callback wrapper name |
options.parameters |
Parameters |
List of arguments that are passed to callback on each iteration |
options.wrapper |
Callback_Wrapper<Value, Result, Parameters, Key> |
Wrapper for callback function that parses inputs and outputs |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
src/asynchronous/iterator-cascade-callbacks.ts:877
▸ skip<Value, Parameters, Key>(amount): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Skip number of iterations
| Name | Type |
|---|---|
Value |
Initial_Iterable_Value |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
amount |
number |
Number of iterations to skip past |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([0, 1, 2, 3, 4, 5]);
(async () => {
const collection = await icca.skip(2).collect([]);
console.log(collection);
//> [ 2, 3, 4, 5 ]
})();src/asynchronous/iterator-cascade-callbacks.ts:918
▸ step<Value, Parameters, Key>(amount): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Step over every n iterations
| Name | Type |
|---|---|
Value |
unknown |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
amount |
number |
Number of iterations to step over |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([0, 1, 2, 3, 4, 5]);
(async () => {
const collection = await icca.step(1).collect([]);
console.log(collection);
//> [ 1, 3, 5 ]
})();src/asynchronous/iterator-cascade-callbacks.ts:957
▸ take<Value, Parameters, Key>(amount): Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Pauses/breaks iteration when limit is reached
| Name | Type |
|---|---|
Value |
unknown |
Parameters |
extends unknown[] = unknown[] |
Key |
unknown |
| Name | Type | Description |
|---|---|---|
amount |
number |
Number of values to compute before pausing |
Iterator_Cascade_Callbacks<Initial_Iterable_Value>
Throws
Notes
- If immediately collecting to an object, consider using
collect()method instead
See
Example
const icca = new Asynchronous.Iterator_Cascade_Callbacks([1, 2, 3, 4]);
icca.take(2);
(async () => {
const collection_one = await icca.collect([]);
console.log(collection_one);
//> [ 1, 2 ]
const collection_two = await icca.collect([]);
console.log(collection_two);
//> [ 3, 4 ]
})();src/asynchronous/iterator-cascade-callbacks.ts:1004
▸ Static zip(...iterables): Iterator_Cascade_Callbacks<unknown>
Returns new instance of Asynchronous.Iterator_Cascade_Callbacks that yields lists of either Shared.Yielded_Data or undefined results
| Name | Type | Description |
|---|---|---|
...iterables |
unknown[] |
List of Generators, Iterators, and/or instances of Asynchronous.Iterator_Cascade_Callbacks |
Iterator_Cascade_Callbacks<unknown>
Notes
- Parameters that are not an instance of
Asynchronous.Iterator_Cascade_Callbackswill be converted - Iteration will continue until all iterables result in
donevalue oftrue
See
Example
Equal Length Iterables
const icca_one = new Asynchronous.Iterator_Cascade_Callbacks([1, 2, 3]);
const icca_two = new Asynchronous.Iterator_Cascade_Callbacks([4, 5, 6]);
const icca_zip = Iterator_Cascade_Callbacks.zip(icca_one, icca_two);
(async () => {
for await (let values of icca_zip) {
console.log('values ->', values);
}
//> values -> [ 1, 4 ]
//> values -> [ 2, 5 ]
//> values -> [ 3, 6 ]
})();Example
Unequal Length Iterables
const icca_three = new Asynchronous.Iterator_Cascade_Callbacks([7, 8, 9]);
const icca_four = new Asynchronous.Iterator_Cascade_Callbacks([10, 11]);
const icca_zip = Iterator_Cascade_Callbacks.zip(icca_three, icca_four);
(async () => {
for await (let values of icca_zip) {
console.log('values ->', values);
}
//> values -> [ 7, 10 ]
//> values -> [ 8, 11 ]
//> values -> [ 9, undefined ]
})();