unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

Document the .tap vs .flatTap fire-and-forget footgun

Open btravers opened this issue 4 months ago • 0 comments

Summary

Document the .tap vs .flatTap fire-and-forget footgun in the choosing-a-combinator guide (and possibly the tap/flatTap TSDoc).

The trap

Because a .tap callback's return is constrained by NotThenable, an effect that returns an AsyncResult (or Promise) cannot be threaded through .tap. If you call such an effect inside .tap, you must discard it in a statement block — and it silently becomes fire-and-forget:

.tap((value) => {
  notifier.onEvent({ ... }); // returns AsyncResult<void, never> — floats, not awaited
})

vs. the await-and-thread form:

.flatTap(() => notifier.onEvent({ ... }))

In a real migration, two call sites of the same effect ended up split across the two forms (one used .tap, the other .flatTap). Both happened to be safe (the effect self-recovers its defects and returns AsyncResult<void, never>), but the asymmetry is easy to introduce by accident and hard to spot in review.

Proposal

  • Add a short callout in docs/guide/choosing-a-combinator.md: "running an effect that itself returns a Result/AsyncResult? use flatTap/flatTapErr, not tap — a tap callback can't return a thenable, so the effect will silently float."
  • Consider echoing one line of this in the tap TSDoc pointing at flatTap.

Docs-only; no API change.

btravers avatar Jul 08 '26 22:07 btravers