tap(callback)
callback<Function>(chunks) => void在每个批次调用一次。- 返回:<Function> 无状态变换。
创建一个能够观察批处理而不修改它们的通道转换。适用于日志记录、指标或调试。
🌐 Create a pass-through transform that observes batches without modifying them. Useful for logging, metrics, or debugging.
import { from, pull, text, tap } from 'node:stream/iter';
const result = pull(
from('hello'),
tap((chunks) => console.log('Batch size:', chunks.length)),
);
console.log(await text(result));const { from, pull, text, tap } = require('node:stream/iter');
async function run() {
const result = pull(
from('hello'),
tap((chunks) => console.log('Batch size:', chunks.length)),
);
console.log(await text(result));
}
run().catch(console.error);tap() 故意不阻止 tapping 回调对块的就地修改;但返回值会被忽略。