Skip to content

Which API to use ​

Use the Execution APIs when calls can be observed or optimized independently. If you need a graph of several calls, choose an engine below.

Picking an engine ​

All three build the same graph. They differ in what else they give you.

If you want…UseAdds
Just the graphTraceableEnginerun(), getTrace()
…plus state shared across stepsExecutionEngineA typed context object
…plus clean call sites@engine and @runDecorators; methods stay normal
ts
const engine = new TraceableEngine();

engine.run(stepA, [1]);
engine.run(stepB, [2]);

engine.getTrace();
ts
const engine = new ExecutionEngine<{ user: User }>();

engine.setContext({ user });
engine.run(stepA, [1]);

engine.getContext();
ts
@engine({ id: 'checkout' })
class Checkout extends EngineTask {
  @run()
  validate(cart: Cart) { /* … */ }
}

run() or @run? ​

Same graph, different ergonomics:

  • engine.run(fn, args) returns the whole ExecutionTrace, so your value is at .outputs. Noisier, but works with any function — including ones you did not write.
  • @run() returns your method's value directly. Cleaner, but needs a class you own that extends EngineTask, and experimentalDecorators.

Decorator or plain function? ​

Tracing, caching, memoization and retry each have a decorator and a plain-function API, backed by the same implementation.

DecoratorPlain function
@traceexecutionTrace
@cacheexecuteCache
@memoizeexecuteMemoize
@retryretry

Use the plain function when you are not writing classes, cannot enable experimentalDecorators, or need to wrap someone else's function. executeCache and executeMemoize additionally require a functionId, since there is no method identity to derive one from. Their stores also live on this, so call them with the same receiver (for example, const scope = { executeMemoize }, then scope.executeMemoize(...)) when calls should share entries.

retry or @retry? ​

Use retry(fn, options?) to create a reusable wrapper, or @retry(options?) on an async method. Both return the function's value in a Promise, preserve arguments and this, and use an independent attempt budget for every invocation.

By default, retry permits three executions with exponential delays of 100 ms and 200 ms after failures. Use shouldRetry to select retryable errors or results. Cancellation is per call: wrapped.withOptions({ signal }, ...args) for a wrapper, or getSignal(args) to select a method argument for a decorator.

Retry does not reuse results or merge concurrent calls. It works without an engine; with { engine } or an inferred this.engine, it records a parent operation and each attempt. See trace integration for composition with @run.

@cache or @memoize? ​

Both optimize execution; neither adds trace records.

@cache@memoize
PurposeReuse results across timeShare one result across near-simultaneous identical calls
TTLYou set it100 ms, capped at 1000 ms; 0 expires promises immediately at settlement
Typical useAn API response reused for 5 minutesRecursion, or 50 callers hitting one endpoint at once
Custom keyYesNo — hashed from inputs
Custom storeYes, e.g. RedisNo — in-memory only

Rule of thumb: use @cache when a result should remain reusable and invalidation matters. Use @memoize when several identical calls arriving together should share one execution. Set ttl: 0 (or @memoize(undefined, 0)) when an asynchronous retry must start fresh after completion or failure. Synchronous results still expire through a timer, including a zero-delay timer with ttl: 0, preserving immediate reuse and recursion.

Released under the MIT License.