Skip to content

Transaction

Transaction<In, Acc, E, EUndo> = WithKind<"Transaction"> & object

Defined in: Core/Transaction.ts:52

A reusable sequence of asynchronous steps with optional actions to undo completed work.

If a step returns an error, completed steps that have an undo action are undone in reverse order. Undo actions can also fail; their errors are returned alongside the original error.

In = void

The initial input payload supplied to the transaction on execution.

Acc extends Record<string, unknown> = Record<never, never>

The accumulated dictionary of step outputs available to downstream steps.

E = never

The union of forward execution error types across all registered steps.

EUndo = never

The union of error types that can occur during compensating undo actions.

readonly [_transactionBrand]: object

readonly _acc: () => Acc

Acc

readonly _error: () => E

E

readonly _in: (_in) => void

In

void

readonly _undoError: () => EUndo

EUndo

const step = Transaction.stepFactory<OrderInput>();
const reservation = step(
  "reservation",
  ({ ctx }) => inventoryApi.reserve(ctx.input.sku, ctx.input.qty),
  ({ ctx }) => inventoryApi.release(ctx.output.id),
);
const payment = step(
  "payment",
  { reservation },
  ({ ctx, deps }) => stripeApi.charge(ctx.input.customerId, deps.reservation.totalCents),
  ({ ctx }) => stripeApi.refund(ctx.output.chargeId),
);
const checkout = Transaction.make(reservation, payment);

const outcome = await Transaction.run(orderInput)(checkout);