effect

Types

The Effect type represents a description of side effects as data. Each effect specifies:

  1. The operations to perform
  2. The type of messages that will be sent back to your program
pub opaque type Effect(msg)

Functions

pub fn dispatch(value: a) -> Effect(a)

Convert a value into an effect that will dispatch that value when performed.

value
|> dispatch
|> perform(handle_value)
pub fn from(effect: fn(fn(a) -> Nil) -> Nil) -> Effect(a)

Create a custom effect from a function that takes a dispatch callback. The dispatch callback can be used to send messages back to your program.

from(fn(dispatch) {
  dispatch(MyMessage)
})
pub fn map(effect: Effect(a), f: fn(a) -> b) -> Effect(b)

Transform the messages produced by an effect. This is useful when you need to adapt effects from one part of your program to work with another.

effect
|> map(fn(msg) { TransformedMessage(msg) })
pub fn none() -> Effect(a)

Create an effect that does nothing. This is useful when you need to return an effect but don’t actually want to perform any operations.

pub fn param(f: fn(a) -> b) -> fn(a) -> b

Helper function similar to clip’s parameter function. Provides an alternative syntax for building curried functions. The following are equivalent:

fn(a) { fn(b) { thing(a, b) } }

{
  use a <- param
  use b <- param
  thing(a, b)
}

Mostly used internally.

pub fn perform(effect: Effect(a), dispatch: fn(a) -> b) -> Nil

Run an effect by providing a dispatch function that will receive any messages produced by the effect.

effect
|> perform(fn(msg) {
  case msg {
    Ok(data) -> handle_success(data)
    Error(e) -> handle_error(e)
  }
})
pub fn try(
  res: Result(a, b),
  f: fn(a) -> Effect(Result(c, b)),
) -> Effect(Result(c, b))

Handle a Result by providing a function that produces an effect for the success case. Errors are automatically converted into effects.

use data <- try(parse_data())
process_data(data)
pub fn try_await(
  pres: Promise(Result(a, b)),
  f: fn(a) -> Effect(Result(c, b)),
) -> Effect(Result(c, b))

Handle a Promise containing a Result by providing a function that produces an effect for the success case. This is particularly useful for handling async operations like HTTP requests.

use response <- try_await(fetch.send(request))
process_response(response)
pub fn try_await_map_error(
  pres: Promise(Result(a, b)),
  map_error: fn(b) -> c,
  f: fn(a) -> Effect(Result(d, c)),
) -> Effect(Result(d, c))

Similar to try_await but allows mapping error values before they’re dispatched. This is commonly used when you want to wrap external errors in your own error type.

use response <- try_await_map_error(
  fetch.send(request),
  fn(e) { NetworkError(e) }
)
process_response(response)
pub fn try_await_replace_error(
  pres: Promise(Result(a, b)),
  e: c,
  f: fn(a) -> Effect(Result(d, c)),
) -> Effect(Result(d, c))

Similar to try_await but allows replacing error values before they’re dispatched. This is commonly used when you want to wrap external errors in your own error type.

use response <- try_await_replace_error(
  fetch.send(request),
  NetworkError,
)
process_response(response)
pub fn try_map_error(
  res: Result(a, b),
  map_error: fn(b) -> c,
  f: fn(a) -> Effect(Result(d, c)),
) -> Effect(Result(d, c))

Similar to try but allows mapping error values before they’re dispatched. You can emulate this using try and result.map_error.

use response <- try_map_error(
  fetch.send(request),
  fn(e) { NetworkError(e) }
)
process_response(response)
pub fn try_replace_error(
  res: Result(a, b),
  e: c,
  f: fn(a) -> Effect(Result(d, c)),
) -> Effect(Result(d, c))

Similar to try but allows replacing error values before they’re dispatched. You can emulate this using try and result.replace_error.

use response <- try_replace_error(
  fetch.send(request),
  NetworkError,
)
process_response(response)
Search Document