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)));