Corner.Promise (corner v0.1.3)

The Promise in Elixir.

Smarily as Promise in Javascript.

Link to this section Summary

Functions

Get value from promise.

Trasform the state of promise with fun.

Dynamic create a new promise with fun.

Create promise with data. The tag can be :resolved or :rejected, default is :resolved.

Add a error handelr for the promise.

Create a :rejected promise.

Create a :resolved promise.

Transform the data or hanlde the error.

Link to this section Types

Link to this type

error_handler()

@type error_handler() :: rejecte_then() | error_then()
Link to this type

error_then()

@type error_then() :: ({:error | :stop, any()} -> any())
Link to this type

rejecte_then()

@type rejecte_then() :: (any() -> any())
@type rejecter() :: (any() -> any())
Link to this type

resolve_then()

@type resolve_then() :: (any() -> any())

The function passed to Promise.then/1-2.

@type resolver() :: (any() -> any())
@opaque t()
@type tag() :: :resolved | :rejected | :error | :stop

Link to this section Functions

@spec await(t()) :: {tag(), any()}

Get value from promise.

Return {tag, value}, where tag is :resolved | :rejected | :error | :stop.

Link to this function

map(promise, fun)

@spec map(t(), resolve_then() | error_handler()) :: t()

Trasform the state of promise with fun.

  • If promise's state on :resolved or :rejected, fun get the value of promise.
  • On :error, fun get {:error, {any, []}}
Link to this function

new(fun, receive_time \\ 60000)

Dynamic create a new promise with fun.

The spectype of fun is (resolver,rejecter)-> any.

  • When resolver is call, promise will turn to :resolved.
  • When rejecter is call, promise will turn to :rejected.
  • When both be called in fun, statue of promise will turn to the one who is first be called.
Link to this function

of(v, tag \\ :resolved)

@spec of(any(), :resolved | :rejected) :: t()

Create promise with data. The tag can be :resolved or :rejected, default is :resolved.

Link to this function

on_error(promise, fun)

@spec on_error(t(), error_handler()) :: t()

Add a error handelr for the promise.

Create a :rejected promise.

Create a :resolved promise.

Link to this function

then(promise, fun1, fun2 \\ nil)

@spec then(t(), nil | resolve_then(), nil | error_handler()) :: t()

Transform the data or hanlde the error.

  • then/2: Transform the data with fun1, but if promise on error, hanlder error whith fun2.
  • then/1: Transform the data with fun1, if promise on error, just skip the fun1.