gossamer/promise

Types

pub type Promise(a)
pub type PromiseWithResolvers(a) {
  PromiseWithResolvers(
    promise: Promise(a),
    resolve: fn(a) -> Nil,
    reject: fn(dynamic.Dynamic) -> Nil,
  )
}

Constructors

Values

pub fn all(values: List(Promise(a))) -> Promise(List(a))

Creates a Promise that is resolved with a list of results when all of the provided Promises resolve, or rejected when any Promise is rejected.

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

Creates a Promise that is resolved with a list of PromiseSettledResults when all of the provided Promises resolve or reject.

pub fn any(values: List(Promise(a))) -> Promise(a)

Returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected.

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

Attaches a callback for only the rejection of the Promise.

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

Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The resolved value cannot be modified from the callback.

pub fn from_option(option: option.Option(a)) -> Promise(a)
pub fn from_result(result: Result(a, r)) -> Promise(a)
pub fn new(
  executor: fn(fn(a) -> Nil, fn(r) -> Nil) -> Nil,
) -> Promise(a)

Creates a new Promise. The executor callback is passed two arguments: a resolve callback used to resolve the promise with a value, and a reject callback used to reject the promise with a provided reason or error.

pub fn race(values: List(Promise(a))) -> Promise(a)

Creates a Promise that is resolved or rejected when any of the provided Promises are resolved or rejected.

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

Creates a new rejected promise for the provided reason.

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

Creates a new resolved promise for the provided value.

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

Attaches a callback for the resolution of the Promise.

pub fn with_resolvers() -> PromiseWithResolvers(a)

Creates a new Promise and returns it in a PromiseWithResolvers record, along with its resolve and reject functions.

Search Document