effect/effect_result

Types

Holds the dispatch function used internally by each effect step. Typically you won’t create Actions directly; it’s used by perform.

pub type Actions(a, e) {
  Actions(dispatch: fn(Result(a, e)) -> Nil)
}

Constructors

  • Actions(dispatch: fn(Result(a, e)) -> Nil)

An effect that can produce either an Ok(a) or an Error(e). Use perform to execute an Effect, providing a callback that receives the final Result(a, e).

pub opaque type Effect(a, e)

Functions

pub fn fail(err: a) -> Effect(b, a)

Create an Effect that immediately fails with error err. Shorthand for from_result(Error(err)).

pub fn from(
  effect: fn(fn(Result(a, b)) -> Nil) -> Nil,
) -> Effect(a, b)

Create an Effect from a callback that manually dispatches Ok or Error. Example:

from(fn(dispatch) {
  dispatch(Ok(42))
})
pub fn from_option(opt: Option(a), if_none: b) -> Effect(a, b)

Create an Effect from an option, mapping the None case into the error channel Useful for wrapping an option into the effect system when None is considered an error.

pub fn from_promise(pres: Promise(Result(a, b))) -> Effect(a, b)

Convert a JavaScript Promise<Result(a, e)> into an Effect(a, e). The Effect will dispatch either Ok(a) or Error(e) based on the resolved promise value.

pub fn from_result(result: Result(a, b)) -> Effect(a, b)

Create an Effect that immediately dispatches a pre-existing Result(a, e). Useful for wrapping a plain result in the effect system.

pub fn map(effect: Effect(a, b), f: fn(a) -> c) -> Effect(c, b)

Transform the success value in an Effect from type a to b. The error type e is left unchanged. Example:

succeed(2)
|> map(fn(x) { x * 2 })  // Ok(4)
pub fn map_error(
  effect: Effect(a, b),
  f: fn(b) -> c,
) -> Effect(a, c)

Transform the error value in an Effect from type e to e2. The success value a is left unchanged. Example:

fail("Oops!")
|> map_error(fn(e) { "Mapped: " <> e })
pub fn perform(
  effect: Effect(a, b),
  callback: fn(Result(a, b)) -> Nil,
) -> Nil

Execute an Effect by supplying the final callback to handle the Result(a, e). Use this to “run” the effect. Example:

let eff = succeed(10)
perform(eff, fn(res) {
  case res {
    Ok(value) -> io.println("Got: \(value)")
    Error(e)  -> io.println("Error: \(e)")
  }
})
pub fn succeed(x: a) -> Effect(a, b)

Create an Effect that immediately succeeds with value x. Shorthand for from_result(Ok(x)).

pub fn try(
  effect: Effect(a, b),
  f: fn(a) -> Effect(c, b),
) -> Effect(c, b)

Chain two Effects. If effect succeeds with Ok(a), call f(a) to produce a new Effect(b, e). If effect fails, propagate the error. Example:

succeed(2)
|> try(fn(x) {
  if x > 0 { succeed(x * 10) }
  else { fail("Can't multiply") }
})
pub fn try_await(
  pres: Promise(Result(a, b)),
  f: fn(a) -> Effect(c, b),
) -> Effect(c, b)

Wait on pres (a Promise<Result(a, e)>) and then, if it succeeds with Ok(a), run f(a). Otherwise, dispatch Error(e).

pub fn try_await_map_error(
  pres: Promise(Result(a, b)),
  f_err: fn(b) -> c,
  f: fn(a) -> Effect(d, c),
) -> Effect(d, c)

Like try_await, but first map the error using f_err. If the promise fails with Error(e), transform it into Error(e2), then call f if successful.

Search Document