peek() function
입력 목록을 반복하며 Iterable/AsyncIterable의 각 요소에 대해 제공된 f를 호출합니다. pipe 내부에서 효과를 생성하려는 경우 사용합니다.
Signature:
typescript
declare function peek<T>(
f: (a: T) => unknown,
iterable: Iterable<T>,
): IterableIterator<T>;Example
ts
const iter = peek((a) => console.log(a), [1, 2, 3, 4]);
iter.next(); // {done:false, value: 1} // log 1
iter.next(); // {done:false, value: 2} // log 2
iter.next(); // {done:false, value: 3} // log 3
iter.next(); // {done:false, value: 4} // log 4
// with pipe
pipe(
[1, 2, 3, 4],
peek((a) => console.log(a)),
toArray,
); // [1, 2, 3, 4] // log 1,2,3,4
await pipe(
Promise.resolve([1, 2, 3, 4]),
peek((a) => console.log(a)),
toArray,
); // [1, 2, 3, 4] // log 1,2,3,4
// if you want to use asynchronous callback
await pipe(
Promise.resolve([1, 2, 3, 4]),
toAsync,
peek(async (a) => console.log(a)),
toArray,
); // [1, 2, 3, 4] // log 1,2,3,4
// with toAsync
await pipe(
[
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
Promise.resolve(4),
],
toAsync,
peek((a) => console.log(a)),
toArray,
); // [1, 2, 3, 4] // log 1,2,3,4