Corner.Promise (corner v0.1.2)

The Promise in Elixir.

Smarily as Promise in Javascript.

Link to this section Summary

Functions

Get value from promise.

Returns a specification to start this module under a supervisor.

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 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())
@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

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

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 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 on error, hanlder error whith fun2.
  • then/1: Transform the data with fun1, if promise on error, just skip the fun1.