Hmbown/CodeWhale · error · TypeError

parallel(): expected an array of thunks

Error message

parallel(): expected an array of thunks

What it means

Thrown by the prelude's `globalThis.parallel` in vm.rs when its first argument is not an array. `parallel(thunks, opts?)` fans out an array of zero-argument async thunk functions, so a non-array first argument is rejected with this TypeError before any scheduling happens.

Solutions

  1. Wrap single thunks in an array: `parallel([thunk])`.
  2. Convert other iterables first: `parallel(Array.from(thunkSet))`.
  3. Ensure the argument order is `parallel(thunks, opts)` — the array comes first.
  4. Pass `[]` explicitly when there is nothing to run rather than undefined/null.

Example fix

// before
const [r] = await parallel(async () => fetchThing());
// after
const [r] = await parallel([async () => fetchThing()]);
Defensive patterns

Strategy: type-guard

Validate before calling

function assertThunks(thunks) {
  if (!Array.isArray(thunks)) {
    throw new TypeError('parallel() requires an array of thunks; wrap a single thunk in [thunk]');
  }
}

Type guard

function isThunkArray(v) {
  return Array.isArray(v) && v.every(t => typeof t === 'function');
}

Try / catch

try {
  const results = await parallel(thunks, opts);
} catch (e) {
  if (e instanceof TypeError && e.message === 'parallel(): expected an array of thunks') {
    console.error('parallel([thunk, ...], opts?) — first argument must be an array');
  } else throw e;
}

Prevention

When it happens

Trigger: Calling `parallel(fn)` with a single thunk instead of `[fn]`; passing an object map or an iterable that is not an Array (e.g. a Set or generator); passing the opts object as the first argument by mistake.

Common situations: Forgot to wrap a single callback in array brackets; converted a Map/Set of thunks without Array.from; argument order swapped so opts landed first.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/fe525006c51340e0. Report an issue: GitHub.

Appendix: source

Thrown at crates/workflow-js/src/vm.rs:1543

    if (opts === null || typeof opts !== "object") {
      throw new TypeError("task(): expected an options object");
    }
    const envelope = JSON.parse(await hostTask(JSON.stringify(opts)));
    if (envelope.error !== undefined) {
      const err = new Error(envelope.error);
      // The host always names the kind; a missing one means an envelope this
      // prelude did not produce, which is not a typed task failure.
      err.kind = HOST_KINDS.indexOf(envelope.error_kind) !== -1
        ? envelope.error_kind
        : SCRIPT_KIND;
      throw err;
    }
    return envelope.value;
  };

  globalThis.parallel = (thunks, opts) => {
    if (!Array.isArray(thunks)) {
      throw new TypeError("parallel(): expected an array of thunks");
    }
    if (thunks.length > MAX_ITEMS) {
      throw new Error("parallel(): max " + MAX_ITEMS + " items per call");
    }
    const mode = slotMode("parallel", opts);
    const failFast = mode === "fail-fast";
    const partial = mode === "partial";
    const errors = [];
    // Returns the slot value, or throws to reject the whole fan-out.
    const onSlotError = (index, err) => {
      const kind = taskErrorKind(err);
      const message = taskErrorText(err);
      stampKind(err, kind);
      // Cancellation is the run's deadline in every mode, partial included.
      if (kind === "cancelled") throw err;
      if (partial) {
        // Opt-in partial mode: every non-cancellation slot failure becomes a
        // structured value the script can branch on. It never masquerades as

View on GitHub (pinned to 73e0f67d83)