gossamer/promise

Types

A proxy for a value that may not be known when the promise is created.

See Promise on MDN.

pub type Promise(a)
pub type PromiseSettledResult(a) {
  Fulfilled(value: a)
  Rejected(reason: dynamic.Dynamic)
}

Constructors

pub type PromiseWithResolvers(a, r) {
  PromiseWithResolvers(
    promise: Promise(Result(a, js_error.JsError)),
    resolve: fn(a) -> Nil,
    reject: fn(r) -> Nil,
  )
}

Constructors

  • PromiseWithResolvers(
      promise: Promise(Result(a, js_error.JsError)),
      resolve: fn(a) -> Nil,
      reject: fn(r) -> Nil,
    )

Values

pub fn all(
  values: List(Promise(a)),
) -> Promise(Result(List(a), js_error.JsError))

Resolves with a list of the fulfilled values when all provided promises fulfill, or rejects as soon as any promise rejects.

pub fn all_settled(
  values: List(Promise(a)),
) -> Promise(List(PromiseSettledResult(a)))

Resolves with a list of PromiseSettledResults when all provided promises have settled (resolved or rejected), never short-circuiting on rejection.

pub fn any(
  values: List(Promise(a)),
) -> Promise(Result(a, js_error.JsError))

Resolves with the first fulfilled promise’s value. Rejects only if all provided promises reject.

pub fn catch(
  promise: Promise(a),
  apply onrejected: fn(dynamic.Dynamic) -> a,
) -> Promise(a)

Chains onrejected to run if promise rejects. The returned promise resolves with the callback’s return value.

pub fn finally(
  promise: Promise(a),
  run onfinally: fn() -> b,
) -> Promise(a)

Chains onfinally to run after promise settles (either fulfills or rejects), without affecting the resolved value.

pub fn from_option(option: option.Option(a)) -> Promise(a)

Converts an Option into a promise. Some becomes resolved; None becomes rejected with Nil.

pub fn from_result(result: Result(a, r)) -> Promise(a)

Converts a Result into a promise. Ok becomes resolved; Error becomes rejected.

pub fn new(
  executor: fn(fn(a) -> b, fn(r) -> c) -> d,
) -> Promise(Result(a, js_error.JsError))

Creates a new Promise by running executor. executor receives resolve and reject callbacks; calling resolve fulfills the promise, calling reject (or throwing) rejects it.

pub fn race(
  values: List(Promise(a)),
) -> Promise(Result(a, js_error.JsError))

Resolves or rejects as soon as any of the provided promises resolves or rejects, taking on that promise’s value.

pub fn reject(reason: r) -> Promise(a)

Creates a promise already rejected with reason.

pub fn resolve(value: a) -> Promise(a)

Creates a promise already fulfilled with value.

pub fn then(
  promise: Promise(a),
  apply onfulfilled: fn(a) -> b,
) -> Promise(b)

Chains onfulfilled to run after promise fulfills. The returned promise resolves with the callback’s return value.

pub fn try(
  func: fn() -> a,
) -> Promise(Result(a, js_error.JsError))

Runs func and wraps the result in a promise. The returned promise resolves with the function’s return value or rejects if it throws or returns a rejecting promise.

pub fn with_resolvers() -> PromiseWithResolvers(a, r)

Creates a promise along with resolve and reject callbacks that can settle it from outside. Useful when the settling logic isn’t known at construction time.

Search Document