promise v0.1.0 Promise View Source

Promise implementation in Elixir.

Hex Version Docs Hex downloads GitHub MIT License


Installation

The package is available in Hex, it can be installed by adding :promise to your list of dependencies in mix.exs:

def application() do
    [
        extra_applications: [
            :promise,
        ],
    ]
end

def deps() do
    [
        {:promise, "~> 0.1.0"},
    ]
end

Link to this section Summary

Functions

Returns a new Promise struct

Returns a new Promise struct

Changes on_error callback of promise

Rejects promise with specified error

Resolves promise

Appends function into promise chain

Link to this section Types

Link to this type t() View Source
t() :: %Promise{chain: term(), on_error: term()}

Link to this section Functions

Link to this function new(on_execution) View Source
new(
  on_execution :: function() | {module(), atom()} | {module(), atom(), [any()]}
) :: Promise.t()

Returns a new Promise struct.

Examples

Promise.new({UserRepository, :all})

Promise.new({UserRepository, :get, [1]})

Promise.new(fn -> UserRepository.get(1) end)

Promise.new(fn ->
    case UserRepository.get(1) do
        %{is_active: false} = user ->
            {:reject, user}
        user ->
            {:resolve, user}
    end
end)
Link to this function new(mod, fun, args \\ []) View Source
new(mod :: atom(), fun :: atom(), args :: [any()]) :: Promise.t()

Returns a new Promise struct.

Examples

Promise.new(UserRepository, :all)

Promise.new(UserRepository, :get, [1])
Link to this function on_error(promise, on_error) View Source
on_error(promise :: Promise.t(), on_error :: function() | {module(), atom()}) ::
  Promise.t()

Changes on_error callback of promise.

Default

fn error -> error end

Examples

Promise.new(UserRepository, :all)
|> Promise.then(fn users -> IO.inspect users end)
|> Promise.on_error(fn error -> IO.inspect(error) end)

Rejects promise with specified error.

Examples

Promise.new(UserRepository, :all)
|> Promise.then(fn users -> IO.inspect users end)
|> Promise.on_error(fn error -> IO.inspect(error) end)
|> Promise.resolve()

Resolves promise.

Examples

Promise.new(UserRepository, :all)
|> Promise.then(fn users -> IO.inspect users end)
|> Promise.on_error(fn error -> IO.inspect(error) end)
|> Promise.resolve()
Link to this function then(promise, on_resolve) View Source
then(promise :: Promise.t(), on_resolve :: function() | {module(), atom()}) ::
  Promise.t()

Appends function into promise chain.

Examples

Promise.new(UserRepository, :all)
|> Promise.then(fn users -> IO.inspect users end)