Skip to main content
An Effect represents a unit of asynchronous work that can emit actions back into the system. Effects are returned from reducers to perform side effects like network requests, timers, database operations, and more.

Type Definition

Creating Effects

None

static var
An effect that does nothing and completes immediately. Useful for situations where you must return an effect, but you don’t need to do anything.

Run

static func
Wraps an asynchronous unit of work that can emit actions any number of times in an effect.Parameters:
  • priority: Priority of the underlying task. If nil, the priority will come from Task.currentPriority
  • name: An optional name to associate with the task that runs this effect
  • operation: The operation to execute
  • handler: An error handler, invoked if the operation throws an error other than CancellationError
Returns: An effect wrapping the given asynchronous work.

Send

static func
Initializes an effect that immediately emits the action passed in.
We do not recommend using Effect.send to share logic. Instead, limit usage to child-parent communication, where a child may want to emit a “delegate” action for a parent to listen to.
static func
Initializes an effect that immediately emits the action passed in with animation.Parameters:
  • action: The action that is immediately emitted by the effect
  • animation: An animation

Usage Examples

Basic Run Effect

Attach to an async sequence in a dependency client:

Error Handling

The closure provided to run is allowed to throw, but any non-cancellation errors thrown will cause a runtime warning. To catch errors, use the catch trailing closure:

Network Request Example

Composing Effects

Merge

static func
Merges a variadic list of effects together into a single effect, which runs the effects at the same time.
Example:
func
Merges this effect and another into a single effect that runs both at the same time.

Concatenate

static func
Concatenates a variadic list of effects together into a single effect, which runs the effects one after the other.
Example:
func
Concatenates this effect and another into a single effect that first runs this effect, and after it completes or is cancelled, runs the other.

Map

func
Transforms all elements from the upstream effect with a provided closure.
Example:

Send Type

A type that can send actions back into the system when used from Effect.run. This type implements callAsFunction so that you invoke it as a function:

Methods

(Action) -> Void
Sends an action back into the system from an effect.
(Action, Animation?) -> Void
Sends an action back into the system from an effect with animation.
(Action, Transaction) -> Void
Sends an action back into the system from an effect with transaction.

Type Aliases

typealias
A convenience type alias for referring to an effect of a given reducer’s domain.
Instead of specifying the action:
You can specify the reducer: