Skip to content

dispatchCommand — run a command handler

Imported from the facade: import { command, dispatchCommand } from "@/framework/facade.js".

Runs a handler registered with command(). Synchronous in-process by default; with async: true it enqueues via the queue instead (dispatchEvent's non-broadcast twin). See Events & Queue.

Signature

FunctionSignatureDescription
dispatchCommand(name, payload, options?) => Promise<any>Run a registered handler now, or enqueue it when async: true

Options:

OptionTypeDefaultDescription
asyncbooleanfalseRun through the queue instead of in-process
queuestring"default"Target queue when async: true

Use cases

Sync by default

ts
const result = await dispatchCommand("calculate-tax", { subtotal: 100 });
// result => { total: 110 }

In the background via the queue

ts
await dispatchCommand("calculate-tax", { subtotal: 100 }, { async: true, queue: "default" });

Notes

  • Looks up a handler registered with command(); returns null for unknown names.
  • With async: true it falls through to queueJob(name, payload, { queue }) — same idempotency/retry semantics as any queued job.
  • Compare with dispatchEvent (adds broadcasting) and queueJob (bare enqueue).

Released under the MIT License.