Aller au contenu principal

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