tap()
tap<
T>(value,interceptor):T
Invokes interceptor with the value, then returns value. Useful for side effects in a pipeline.
๐ Why is this a Hidden Gem?
Peek into a chain or pipeline for debugging without affecting the data flow.
DEPRECATED
Use inline side effects in pipe or .then() directly instead.
Type Parametersโ
T: Tโ
The type of the value.
Parametersโ
value: Tโ
The value to pass to interceptor.
interceptor: (value) => voidโ
The function to invoke.
Returns: Tโ
The original value.
Sinceโ
2.0.0
Also known asโ
tap (Lodash, Remeda, Ramda, Effect) ยท โ (es-toolkit, Radashi, Modern Dash, Antfu)
Exampleโ
// โ Deprecated approach
import { pipe } from '@pithos/arkhe/function/pipe';
pipe(
[1, 2, 3],
arr => tap(arr, x => console.log('before:', x)),
arr => arr.map(n => n * 2),
arr => tap(arr, x => console.log('after:', x))
);
// โ
Recommended approach
pipe(
[1, 2, 3],
arr => { console.log('before:', arr); return arr; },
arr => arr.map(n => n * 2),
arr => { console.log('after:', arr); return arr; }
);
// Or extract to a helper
const log = `<T>`(label: string) => (value: T): T => {
console.log(label, value);
return value;
};
How it works?โ
Invokes interceptor and returns the value. Deprecated: Use inline function or console.log directly.
Native Equivalentโ
// โ tap(value, console.log)
// โ
(console.log(value), value)
Use Casesโ
Debug in chain ๐โ
Inspect value in method chain.
const result = data
.filter(x => x > 0)
.map(x => x * 2)
.tap(console.log) // Native: just inline
// Use: .map(x => (console.log(x), x))
.reduce((a, b) => a + b);
Side effect in pipelineโ
Perform side effect without changing value.
const withLog = arr
.filter(Boolean)
.map(x => { console.log(x); return x; });
Inline inspectionโ
Use comma operator for inline debugging.
arr.map(x => (console.log(x), transform(x)));