SDK reference
SDK reference

operation

Measure a named unit of work and record its duration.

Signatures

run.operation(name: string, options?: WorkflowOperationOptions): WorkflowOperation
run.operation<T>(
  name: string,
  callback: () => PromiseLike<T>,
  options?: WorkflowOperationOptions,
): Promise<T>

Promise callback

const receipt = await run.operation("charge-payment", () => chargeCustomer())

The SDK starts an operation, closes it when the callback settles, and preserves its return value. If it rejects, the operation is marked failed and the same original cause is rethrown. A thrown { code, message?, retryable? } is copied into telemetry. An Error is recorded as operation_failed plus its message. Synchronous callback throws are also recorded as operation failures.

options.properties attach bounded JSON to the operation start event, the same way step properties stay on that step.

If the operation fails, decide whether to recover or call fail() for the run.

Explicit handle

const operation = run.operation("reserve-stock", {
  properties: { warehouse: "eu-west" },
})
try {
  await reserveStock()
  operation.done()
} catch (error) {
  operation.fail({ code: "OUT_OF_STOCK", retryable: false })
  throw error
}
MemberMeaning
operationIdUnique UUID for this operation's events
done(): voidRecord success and monotonic duration
fail(failure): voidRecord a structured failure and monotonic duration

failure is { code, message?, retryable? }. code is required. Do not include sensitive exception messages.

Overlap and completion

Operations are independently identified siblings. They may overlap and share a name; nested operation relationships are not supported.

If run.done() is called while operations remain open, run completion is deferred until the final handle closes. The first completion request and result win. Other run markers are then ignored, but the existing handles can close. An unclosed handle can leave the run open indefinitely. run.fail() supersedes pending completion.

Calling an operation callback after the run is terminal still executes your callback. The run stays closed.

On this page