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
error_handler()
@type error_handler() :: rejecte_then() | error_then()
error_then()
rejecte_then()
rejecter()
resolve_then()
resolver()
@opaque t()
tag()
@type tag() :: :resolved | :rejected | :error | :stop
Link to this section Functions
await(map)
Get value from promise
.
Return {tag, value}
, where tag is :resolved | :rejected | :error | :stop
.
child_spec(init_arg)
Returns a specification to start this module under a supervisor.
See Supervisor
.
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, []}}
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.
of(v, tag \\ :resolved)
Create promise with data.
The tag
can be :resolved
or :rejected
, default is :resolved
.
on_error(promise, fun)
@spec on_error(t(), error_handler()) :: t()
Add a error handelr for promise.
reject(v)
Create a :rejected
promise.
resolve(v)
Create a :resolved
promise.
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 withfun1
, but if on error, hanlder error whithfun2
.then/1
: Transform the data withfun1
, if promise on error, just skip thefun1
.