Skip to content

Transaction

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

Defined in: Core/Transaction.ts:67

An immutable, transactional workflow descriptor coordinating a linear sequence of asynchronous steps with automatic reverse (LIFO) rollback upon failure.

Each step receives the accumulated outputs of all preceding successful steps, and can optionally register an undo action. If any step fails or times out, all previously completed steps are undone in strict reverse order to maintain consistency across external services.

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 checkout = pipe(
  Transaction.make<OrderInput>(),
  Transaction.step({
    id: "reservation",
    run: ({ initial }) => inventoryApi.reserve(initial.sku, initial.qty),
    undo: (res) => inventoryApi.release(res.id),
  }),
  Transaction.step({
    id: "payment",
    run: ({ initial, reservation }) => stripeApi.charge(initial.customerId, reservation.totalCents),
    undo: (pay) => stripeApi.refund(pay.chargeId),
  }),
  Transaction.step({
    id: "shipment",
    run: ({ initial, reservation, payment }) => shippingApi.dispatch(initial.address, reservation.id, payment.chargeId),
  }),
);

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