runWithCorrelation<T> static method

Future<T> runWithCorrelation<T>(
  1. Future<T> fn(), {
  2. String? correlationId,
})

Runs the given function with a new correlation ID.

All code executed within fn (including async operations) will have access to the same correlation ID via current.

If correlationId is provided, it will be used; otherwise, a new one will be generated.

Implementation

static Future<T> runWithCorrelation<T>(
  Future<T> Function() fn, {
  String? correlationId,
}) async {
  final id = correlationId ?? _generateId();

  return await runZoned(fn, zoneValues: {_correlationIdKey: id});
}