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.
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
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
struct Send<Action>
A type that can send actions back into the system when used from
run(priority:operation:catch:fileID:filePath:line:column:)
.