withEditTxn Function

Execute a callback within an explicit editing transaction. A new EditTxn is created, started, and passed to fn. If fn returns normally (or its returned Promise resolves), the transaction is committed. If fn throws (or its returned Promise rejects), the transaction is abandoned — none of the changes made during the callback are saved — and the error is re-thrown.

This is the recommended way to perform a scoped unit of work on an iModel. It ensures that edits are committed atomically on success and rolled back on failure, without the caller needing to manage start / end manually.

withEditTxn<T>(iModel: IModelDb, fn: (txn: EditTxn) => T): T

@returns The value returned by fn.

@throws EditTxnError if the transaction cannot be started (e.g. unsaved changes or another EditTxn is active).

@throws Re-throws any error thrown by fn after abandoning the transaction.

Parameter Type Description
iModel IModelDb The iModel to edit.
fn (txn: EditTxn) => T A callback that receives the active EditTxn and performs edits.

Returns - T

The value returned by fn.Execute a callback within an explicit editing transaction, supplying commit arguments.

withEditTxn<T>(iModel: IModelDb, saveArgs: string | SaveChangesArgs, fn: (txn: EditTxn) => T): T

@returns The value returned by fn.

Parameter Type Description
iModel IModelDb The iModel to edit.
saveArgs string | SaveChangesArgs Description or structured arguments passed to saveChanges on save.
fn (txn: EditTxn) => T A callback that receives the active EditTxn and performs edits.

Returns - T

The value returned by fn.Execute an async callback within an explicit editing transaction.

withEditTxn<T>(iModel: IModelDb, fn: (txn: EditTxn) => Promise<T>): Promise<T>

@returns A Promise that resolves to the value returned by fn.

Parameter Type Description
iModel IModelDb The iModel to edit.
fn (txn: EditTxn) => Promise<T> An async callback that receives the active EditTxn and performs edits.

Returns - Promise<T>

A Promise that resolves to the value returned by fn.Execute an async callback within an explicit editing transaction, supplying commit arguments.

withEditTxn<T>(iModel: IModelDb, saveArgs: string | SaveChangesArgs, fn: (txn: EditTxn) => Promise<T>): Promise<T>

@returns A Promise that resolves to the value returned by fn.

Parameter Type Description
iModel IModelDb The iModel to edit.
saveArgs string | SaveChangesArgs Description or structured arguments passed to saveChanges on save.
fn (txn: EditTxn) => Promise<T> An async callback that receives the active EditTxn and performs edits.

Returns - Promise<T>

A Promise that resolves to the value returned by fn.

Defined in

Last Updated: 28 April, 2026