Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:A5c ai Babysitter Parallel Promise Instead Of Thunk Recovery

From Leeroopedia
Knowledge Sources
Domains Debugging, Runtime
Last Updated 2026-10-23 00:00 GMT

Overview

Recover from promise-instead-of-thunk misuse in `parallel.all()` by wrapping orphaned promises as thunks to prevent unhandled rejection crashes.

Description

When callers write `[ctx.task(...)]` instead of `[() => ctx.task(...)]`, the promises are already executing and will reject with `EffectRequestedError` as orphaned unhandled rejections, crashing the Node.js process. The `runParallelAll` function detects non-function elements that are thenable (have a `.then` method) and converts them to thunks wrapping the existing promises. This allows the batch-collection loop to still gather all pending effect actions without orphan rejections. Non-thenable non-function elements (e.g., `null`, numbers) still throw a `TypeError`.

Usage

Use this heuristic when debugging Node.js process crashes with unhandled promise rejections during parallel task execution. The symptom is an `EffectRequestedError` rejection appearing as an orphan when `parallel.all()` is called with an array of already-invoked promises instead of thunks.

The Insight (Rule of Thumb)

  • Action: Detect thenable non-function elements in the `thunks` array and wrap them as `() => promise` thunks.
  • Value: `thunks[i] = (() => promise) as unknown as () => T | Promise<T>` when `typeof thunks[i] !== 'function'` and `thunks[i].then === 'function'`.
  • Trade-off: The recovery is silent — callers should use thunks `[() => ctx.task(...)]` for correct behavior, but the recovery prevents crashes from misuse. Non-thenable non-functions still throw `TypeError`.
  • Compatibility: The shallow copy of the thunks array avoids mutating the caller's original array.

Reasoning

The parallel batch collection pattern relies on thunks so that effect requests (`EffectRequestedError`, `EffectPendingError`) are caught and collected rather than becoming orphan rejections. When a promise is already executing (not wrapped in a thunk), its rejection happens outside the try/catch of the batch loop, creating an unhandled rejection that crashes Node. By wrapping the existing promise in a thunk, the rejection is caught within the batch loop's try/catch, and the pending effect action is collected properly.

Code Evidence

Recovery logic from `packages/babysitter-sdk/src/runtime/intrinsics/parallel.ts:14-38`:

// Detect and recover from promise-instead-of-thunk misuse.  When callers
// write [ctx.task(...)] instead of [() => ctx.task(...)], the promises are
// already executing and will reject with EffectRequestedError as orphaned
// unhandled rejections (crashing Node).  Convert them to thunks wrapping the
// existing promises so the batch-collection loop can still gather all pending
// effect actions without orphan rejections.
let recovered = false;
for (let i = 0; i < thunks.length; i++) {
  if (typeof thunks[i] !== "function") {
    const isThenable =
      thunks[i] && typeof (thunks[i] as unknown as { then?: unknown }).then === "function";
    if (isThenable) {
      if (!recovered) {
        thunks = [...thunks]; // shallow copy to avoid mutating caller's array
        recovered = true;
      }
      const promise = thunks[i] as unknown as Promise<T>;
      thunks[i] = (() => promise) as unknown as () => T | Promise<T>;
    } else {
      throw new TypeError(
        `parallel.all() expects an array of functions (thunks), but element at index ${i} is ${typeof thunks[i]}. Wrap each entry as a function: [() => ctx.task(...)] instead of [ctx.task(...)]`
      );
    }
  }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment