run(priority:operation:catch:fileID:filePath:line:column:)

Wraps an asynchronous unit of work that can emit actions any number of times in an effect.

Effect.swift:87EffectRun.md
static func run(priority: TaskPriority? = nil, operation: @escaping (_ send: Send<Action>) async throws -> Void, catch handler: ((_ error: any Error, _ send: Send<Action>) async -> Void)? = nil, fileID: StaticString = #fileID, filePath: StaticString = #filePath, line: UInt = #line, column: UInt = #column) -> Effect<Action>

Parameters

priority

Priority of the underlying task. If nil, the priority will come from Task.currentPriority.

operation

The operation to execute.

handler

An error handler, invoked if the operation throws an error other than CancellationError.

fileID

The fileID.

filePath

The filePath.

line

The line.

column

The column.

Returns

An effect wrapping the given asynchronous work.

For example, if you had an async sequence in a dependency client:

struct EventsClient {
  var events: () -> any AsyncSequence<Event, Never>
}

Then you could attach to it in a run effect by using for await and sending each action of the stream back into the system:

case .startButtonTapped:
  return .run { send in
    for await event in self.events() {
      send(.event(event))
    }
  }

See Send for more information on how to use the send argument passed to run’s closure.

The closure provided to run(priority:operation:catch:fileID:filePath:line:column:) is allowed to throw, but any non-cancellation errors thrown will cause a runtime warning when run in the simulator or on a device, and will cause a test failure in tests. To catch non-cancellation errors use the catch trailing closure.

Sending actions