Pinky v0.2.0 Pinky

Pinky is a promise library for Elixir.

Summary

Functions

Takes a list of promises and returns a promise that will resolve only when all of them are successfully resolved. If one of them is rejected, the returned promise will be rejected too

Extracts the value (or the error) of a promise. It will block the caller process until the promise is realized

Applies a function to the eventual result of a promise (when and if it’s resolved successfully), assuming the function will return another promise, and returns a promise that will evaluate to the result of the inner promise

Applies a function to the eventual result of a promise (when and if it’s resolved successfully), and returns a promise that will evaluate to that result

Constructs a promise with a function that will run in a separate process

Constructs a promise already rejected with a predefined error

Constructs a promise already resolved with a predefined value

Takes a list of promises and returns a promise that will resolve only when all of them are successfully resolved. If one of them is rejected, the returned promise will be rejected too

Functions

all(promises)

Takes a list of promises and returns a promise that will resolve only when all of them are successfully resolved. If one of them is rejected, the returned promise will be rejected too.

Examples

iex> Pinky.all([Pinky.resolved(3), Pinky.resolved(5)]) |> Pinky.extract
{:ok, [3, 5]}

iex> Pinky.all([Pinky.rejected("error"), Pinky.resolved(5)]) |> Pinky.extract
{:error, "Some promises failed."}
extract(promise)

Extracts the value (or the error) of a promise. It will block the caller process until the promise is realized.

Examples

iex> Pinky.extract(Pinky.promise(fn -> 1 + 2 end))
{:ok, 3}
flat_map(promise, f)

Applies a function to the eventual result of a promise (when and if it’s resolved successfully), assuming the function will return another promise, and returns a promise that will evaluate to the result of the inner promise.

Examples

iex> Pinky.resolved(3) |>
...> Pinky.flat_map(fn x -> Pinky.promise(fn -> x + 1 end) end) |>
...> Pinky.extract
{:ok, 4}

iex> Pinky.rejected("outer failed") |>
...> Pinky.flat_map(fn x -> Pinky.promise(fn -> x + 1 end) end) |>
...> Pinky.extract
{:error, "outer failed"}

iex> Pinky.resolved(3) |>
...> Pinky.flat_map(fn x ->
...>                if x > 2 do
...>                  Pinky.rejected("inner failed")
...>                else
...>                  Pinky.promise(fn -> x + 1 end)
...>                end
...>               end) |>
...> Pinky.extract
{:error, "inner failed"}
map(promise, f)

Applies a function to the eventual result of a promise (when and if it’s resolved successfully), and returns a promise that will evaluate to that result.

Examples

iex> Pinky.resolved(3) |> Pinky.map(fn x -> x + 1 end) |> Pinky.extract
{:ok, 4}

iex> Pinky.rejected("hell") |> Pinky.map(fn x -> x + 1 end) |> Pinky.extract
{:error, "hell"}
promise(f)

Constructs a promise with a function that will run in a separate process.

Examples

iex> Pinky.extract(Pinky.promise(fn -> 1 + 2 end))
{:ok, 3}
rejected(e)

Constructs a promise already rejected with a predefined error.

Examples

iex> Pinky.extract(Pinky.rejected("something went wrong"))
{:error, "something went wrong"}
resolved(v)

Constructs a promise already resolved with a predefined value.

Examples

iex> Pinky.extract(Pinky.resolved(3))
{:ok, 3}
some(promises)

Takes a list of promises and returns a promise that will resolve only when all of them are successfully resolved. If one of them is rejected, the returned promise will be rejected too.

Examples

iex> Pinky.some([Pinky.resolved(3), Pinky.resolved(5)]) |> Pinky.extract
{:ok, [3, 5]}

iex> Pinky.some([Pinky.rejected("error"), Pinky.resolved(5)]) |> Pinky.extract
{:ok, [5]}