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