writeAsync<T> method

Future<T> writeAsync<T>(
  1. T writeCallback(), [
  2. CancellationToken? cancellationToken
])

Executes the provided writeCallback in a temporary write transaction. Both acquiring the write lock and committing the transaction will be done asynchronously.

Implementation

Future<T> writeAsync<T>(T Function() writeCallback, [CancellationToken? cancellationToken]) async {
  assert(!_isFuture<T>(), 'writeCallback must be synchronous');
  final transaction = await beginWriteAsync(cancellationToken);
  try {
    T result = writeCallback();
    await transaction.commitAsync(cancellationToken);
    return result;
  } catch (e) {
    if (isInTransaction) {
      transaction.rollback();
    }
    rethrow;
  }
}