Function: inspect()
inspect<
ElementType
>(iterable
:IterableResolvable
<ElementType
>,callbackFn
: (element
:ElementType
,index
:number
) =>void
):IterableIterator
<ElementType
>
Does something with each element of an iterator, passing the value on.
When using iterators, you'll often chain several of them together. While working on such code, you might want to check out what's happening at various parts in the pipeline. To do that, insert a call to this function.
It's more common for this function to be used as a debugging tool than to exist in your final code, but applications may find it useful in certain situations when errors need to be logged before being discarded.
Type Parameters
Type Parameter |
---|
ElementType |
Parameters
Parameter | Type | Description |
---|---|---|
iterable | IterableResolvable <ElementType > | An iterator to inspect. |
callbackFn | (element : ElementType , index : number ) => void | - |
Returns
IterableIterator
<ElementType
>
Example
import { inspect } from '@sapphire/iterator-utilities';
const iterable = [1, 4, 2, 3];
let iter = inspect(iter, (value) => console.log(`about to filter: ${value}`));
iter = filter(iterable, (value) => value % 2 === 0);
iter = inspect(iter, (value) => console.log(`made it through filter: ${value}`));
const sum = reduce(iter, (acc, value) => acc + value, 0);
console.log(sum);
// Output:
// about to filter: 1
// about to filter: 4
// made it through filter: 4
// about to filter: 2
// made it through filter: 2
// about to filter: 3
// 6
Defined in
projects/utilities/packages/iterator-utilities/src/lib/inspect.ts:39