Skip to content

0.18 PR5: support async stopWhen predicates - #28

Merged
ildella merged 2 commits into
masterfrom
fix-stopwhen-async
Nov 24, 2026
Merged

ildella merged 2 commits into
masterfrom
fix-stopwhen-async

Conversation

@ildella

@ildella ildella commented Nov 24, 2026

Copy link
Copy Markdown
Owner

What / why

stopWhen called the predicate without awaiting it. An async predicate returned a Promise — always truthy — so iteration stopped on the first item, silently. stopWhen now awaits predicate(item, index) before deciding to yield, so async () => false keeps every item and async () => true stops correctly.

A rejecting async predicate keeps the existing source-error behaviour: it surfaces through onSourceError / sourceErrors, never onError.

stopWhenSync now rejects a thenable predicate result through the existing assertSyncCallback helper. The error is a TypeError naming the async alternative:

stopWhenSync() predicate returned a Promise; use stopWhen()

stopWhen stays a truthiness stop predicate: drop is not special there (that is PR 6).

Tests

  • tests/stop-when.test.js: async false → all items; async true → stop; await happens before yield (index still (item, index)); rejecting async predicate consumed by series → source error.
  • tests/stop-when-sync.test.js: async / Promise-returning predicate → TypeError naming stopWhen().
  • Sync predicates unchanged; all 938 existing tests pass.

Verification

  • yarn lint — clean, 0 warnings
  • yarn vitest run — 71 files, 938 tests passed
  • yarn check — duplicate/clone and knip clean

Docs

docs/reference.md#stopwhen (sync or async), #stopwhensync (sync only, thenable rejected), and the guide's source-adapters section.

Plan: .opencode/plan/pipelean-0.18.md, PR 5.

stopWhen checked the predicate without awaiting it, so an async predicate
returned a Promise — always truthy — and stopped the iteration on the
first item. Await the result before deciding to yield.

stopWhenSync now rejects a thenable predicate result via
assertSyncCallback, producing a TypeError that names stopWhen() as the
async alternative instead of silently treating a Promise as truthy.

Docs updated for the new contract in reference and guide.
@ildella
ildella marked this pull request as ready for review November 24, 2026 14:28

@ildella ildella left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVED

REVIEW: Both review passes found no medium-or-higher issues. stopWhen now awaits its predicate — a Promise<false> keeps the item, a truthy result stops before the item is yielded — and stopWhenSync rejects thenables through the existing assertSyncCallback helper with a TypeError naming stopWhen(). A rejecting async predicate surfaces as a source error (not onError), consistent with how sync predicate throws already behave. Guide and reference docs cover both functions.

FINDINGS: No medium-or-higher findings. Low/info follow-ups:

  • LOW — await predicate(...) defers each sync predicate's result by one microtask. Trigger: any timing-sensitive consumer of a fully sync stopWhen source; no library consumer or existing test observes it, and the predicate call itself stays synchronous after the pull.
  • LOW — four new tests repeat the same async () => { await Promise.resolve(); ... } fixture. Trigger: changing how a "genuinely async" predicate is built requires four edits; test-only duplication, not duplicated production derivation.
  • INFO — tests/stop-when-sync.test.js asserts the exact error string. Trigger: rephrasing the shared message in src/shared.js breaks the test despite unchanged behavior; this matches established repo convention.
  • INFO — the guide says "when it returns a Promise", while await also unwraps arbitrary thenables. Trigger: a thenable predicate; code handles it, wording is narrower. Cosmetic.
  • INFO — out of scope and pre-existing: under strategy: 'throw', a predicate that throws undefined rethrows undefined verbatim.

CI: green — the test check (Code Check workflow) succeeded.

@ildella
ildella merged commit f333e6d into master Nov 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant