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
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
Wraps an asynchronous unit of work that can emit actions any number of times in an effect.Parameters:
priority: Priority of the underlying task. Ifnil, the priority will come fromTask.currentPriorityname: An optional name to associate with the task that runs this effectoperation: The operation to executehandler: An error handler, invoked if the operation throws an error other thanCancellationError
Send
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.Initializes an effect that immediately emits the action passed in with animation.Parameters:
action: The action that is immediately emitted by the effectanimation: An animation
Usage Examples
Basic Run Effect
Attach to an async sequence in a dependency client:Error Handling
The closure provided torun 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
Merges a variadic list of effects together into a single effect, which runs the effects at the same time.Example:
Merges this effect and another into a single effect that runs both at the same time.
Concatenate
Concatenates a variadic list of effects together into a single effect, which runs the effects one after the other.Example:
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
Transforms all elements from the upstream effect with a provided closure.Example:
Send Type
Effect.run. This type implements callAsFunction so that you invoke it as a function:
Methods
Sends an action back into the system from an effect.
Sends an action back into the system from an effect with animation.
Sends an action back into the system from an effect with transaction.
Type Aliases
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: