unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

A lint rule for the eager-AsyncResult sequence: sibling consts that were meant to be sequential

Open btravers opened this issue 3 months ago • 1 comments

Raised from btravstack/start, where the trap is felt — see btravstack/start#68 and its fix.

The trap

An AsyncResult is eager: constructing it starts the work. So the spelling a reader reaches for first — one const per step, then chained — runs the steps concurrently, while still type-checking and still returning a Result.

Measured, on [email protected]:

const a = step("a", 30), b = step("b", 5);
await a.flatMap(() => b);
// start:a start:b end:b end:a      ← a race

await DoAsync().bind("a", () => step("a", 30)).bind("b", () => step("b", 5));
// start:a end:a start:b end:b      ← sequential

The library already answers it twice — flatTap where a later step needs only the earlier one's success, DoAsync().bind(...) where it needs its value — so this is not a request for a new combinator. It is that nothing tells you, and the failure is silent.

The ask: a lint rule in @unthrown/oxlint

Something like no-eager-sequence. What makes it worth building is that the mistake is invisible at runtime until timing matters, and a linter is the only place it can be caught — the types are identical either way.

The discriminator, which is the hard part

Sibling construction is not itself wrong. It is exactly how deliberate concurrency is spelled:

const a = fetchA(), b = fetchB();
await allAsync([a, b]);        // correct, and the point

So the rule cannot flag sibling AsyncResult construction. It has to flag sibling construction followed by sequential consumption — an AsyncResult bound to a const whose first use is inside another Result's callback (flatMap, flatTap, andThen, a Do bind). Consumed by allAsync, allFromDictAsync, or awaited directly, it is fine.

A first cut could be narrower still and catch most of it: a const holding a call that returns AsyncResult, used exactly once, inside a callback passed to a method on another AsyncResult in the same scope.

Prior art in the same family

unthrown/no-catch-all-pattern and unthrown/prefer-async-result already encode "this compiles but is not what you meant". This is the same shape, and arguably a sharper case, since the consequence is a concurrency bug rather than a weaker type.

What the consuming repo did meanwhile

btravstack/start rewrote its two sagas with flatTap, recorded the rule in its own CLAUDE.md and on three documentation pages, and leaned on specs that assert step ordering so a regression fails a test. That closes it there. The lint rule is what would close it for everyone else, which is why it belongs here rather than downstream.

btravers avatar Aug 20 '26 20:08 btravers

Fixed by #248, released in @unthrown/[email protected]: unthrown/no-async-result-race, in the recommended preset. The discriminator is the one asked for here — sibling construction is flagged only when consumed sequentially (a const used inside another AsyncResult's callback, or start-both-await-both as separate awaits), while allAsync([a, b]) and a direct await stay sanctioned; the rule's tests pin both sides, including chains rooted at a producer and renamed imports. The consuming repo that raised this closed its own loop in btravstack/start#99: adopting the rule fired on nothing (the flatTap/DoAsync discipline was already kept) and its CLAUDE.md's admission that nothing in the gate catches the race is retired.

btravers avatar Aug 24 '26 13:08 btravers