Executors
An executor is a synchronous command. The caller passes it to postboy.exec(...) and gets the typed result back immediately — no stream, no waiting. Executors extend PostboyExecutor<T>. For results that arrive asynchronously use callbacks; for notifications use events.
When to use executors
Use executors for operations that complete immediately: mapping, formatting, validation, cache lookups, lightweight commands. Do not use them for asynchronous work — exec returns T itself, so a Promise would come back unresolved. Do not use them for broadcasts — subscribers cannot react to an executor.
The contract
The class states its identity (ID), its input (constructor payload), and its output (T). Read the class and you know the operation.
Execute with exec
exec is synchronous and returns T directly. Never await it and never treat its result as an Observable. If the operation is asynchronous, model it as a PostboyCallbackMessage<T> and send it with fireCallback — see Callbacks.
Registration
An executor must be registered before use; an unregistered ID throws at the call site.
Direct function via ConnectExecutor:
Handler class via ConnectHandler — for logic that outgrows a lambda:
Inside a registrator, use the chainable forms (not deprecated):
Executors vs events vs callbacks
Event | Callback | Executor | |
|---|---|---|---|
Class |
|
|
|
Verb |
|
|
|
Result | none |
|
|
Consumers | zero or many subscribers | one responder completes the message | exactly one registered handler |
Timing | broadcast, asynchronous | request/response, asynchronous | immediate |
Middleware
exec passes through the middleware pipeline like every dispatch. An Interrupt returned from before(...) throws CancelError instead of running the handler — see Middleware.
Pitfalls
await postboy.exec(...)— the result isT, not a Promise.Calling
execbefore registration — throws (TypeErrorcalling an undefined handler).Long-running or asynchronous work inside a handler — it blocks the caller or returns an unresolved Promise.
Sharing one
IDbetween two executor classes — the last registration wins.
Next steps
Executor Patterns — errors, lifecycle, best practices