Current section
Files
Jump to
Current section
Files
src/gleam@javascript@promise.erl
-module(gleam@javascript@promise).
-compile(no_auto_import).
-export([resolve/1, rescue/2, await/2, map/2, tap/2, map_try/2, try_await/2, await2/2, await3/3, await4/4, await5/5, await6/6, await_array/1, await_list/1]).
-export_type([promise/1]).
-type promise(Value) :: any() | {gleam_phantom, Value}.
-spec resolve(EOW) -> promise(EOW).
resolve(Field@0) ->
'../../ffi.mjs':resolve(Field@0).
-spec rescue(promise(EOY), fun((gleam@dynamic:dynamic()) -> EOY)) -> promise(EOY).
rescue(Field@0, Field@1) ->
'../../ffi.mjs':rescue(Field@0, Field@1).
-spec await(promise(EPD), fun((EPD) -> promise(EPB))) -> promise(EPB).
await(Field@0, Field@1) ->
'../../ffi.mjs':then(Field@0, Field@1).
-spec map(promise(EPI), fun((EPI) -> EPG)) -> promise(EPG).
map(Field@0, Field@1) ->
'../../ffi.mjs':map_promise(Field@0, Field@1).
-spec tap(promise(EPK), fun((EPK) -> any())) -> promise(EPK).
tap(Promise, Callback) ->
_pipe = Promise,
'../../ffi.mjs':map_promise(
_pipe,
fun(A) ->
Callback(A),
A
end
).
-spec map_try(
promise({ok, EPO} | {error, EPP}),
fun((EPO) -> {ok, EPT} | {error, EPP})
) -> promise({ok, EPT} | {error, EPP}).
map_try(Promise, Callback) ->
_pipe = Promise,
'../../ffi.mjs':map_promise(_pipe, fun(Result) -> case Result of
{ok, A} ->
Callback(A);
{error, E} ->
{error, E}
end end).
-spec try_await(
promise({ok, EPZ} | {error, EQA}),
fun((EPZ) -> promise({ok, EQE} | {error, EQA}))
) -> promise({ok, EQE} | {error, EQA}).
try_await(Promise, Callback) ->
_pipe = Promise,
'../../ffi.mjs':then(_pipe, fun(Result) -> case Result of
{ok, A} ->
Callback(A);
{error, E} ->
'../../ffi.mjs':resolve({error, E})
end end).
-spec await2(promise(EQL), promise(EQM)) -> promise({EQL, EQM}).
await2(Field@0, Field@1) ->
'../../ffi.mjs':all_promises(Field@0, Field@1).
-spec await3(promise(EQQ), promise(EQR), promise(EQS)) -> promise({EQQ,
EQR,
EQS}).
await3(Field@0, Field@1, Field@2) ->
'../../ffi.mjs':all_promises(Field@0, Field@1, Field@2).
-spec await4(promise(EQX), promise(EQY), promise(EQZ), promise(ERA)) -> promise({EQX,
EQY,
EQZ,
ERA}).
await4(Field@0, Field@1, Field@2, Field@3) ->
'../../ffi.mjs':all_promises(Field@0, Field@1, Field@2, Field@3).
-spec await5(
promise(ERG),
promise(ERH),
promise(ERI),
promise(ERJ),
promise(ERK)
) -> promise({ERG, ERH, ERI, ERJ, ERK}).
await5(Field@0, Field@1, Field@2, Field@3, Field@4) ->
'../../ffi.mjs':all_promises(Field@0, Field@1, Field@2, Field@3, Field@4).
-spec await6(
promise(ERR),
promise(ERS),
promise(ERT),
promise(ERU),
promise(ERV),
promise(ERW)
) -> promise({ERR, ERS, ERT, ERU, ERV, ERW}).
await6(Field@0, Field@1, Field@2, Field@3, Field@4, Field@5) ->
'../../ffi.mjs':all_promises(Field@0, Field@1, Field@2, Field@3, Field@4, Field@5).
-spec await_array(gleam@javascript@array:array(promise(ESE))) -> promise(gleam@javascript@array:array(ESE)).
await_array(Field@0) ->
'../../ffi.mjs':all_promises(Field@0).
-spec await_list(list(promise(ESJ))) -> promise(list(ESJ)).
await_list(Xs) ->
_pipe = Xs,
_pipe@1 = '../../ffi.mjs':all_promises(_pipe),
'../../ffi.mjs':map_promise(_pipe@1, fun gleam@javascript@array:to_list/1).