README.md

# Multitask

[![Build Status](https://travis-ci.org/fertapric/multitask.svg?branch=master)](https://travis-ci.org/fertapric/multitask)

Multitasks are processes meant to execute several functions asynchronously, and collect
their return values when they are done processing or the error of the first failure.

## Installation

Add `multitask` to your project's dependencies in `mix.exs`:

```elixir
def deps do
  [{:multitask, "~> 0.1"}]
end
```

And fetch your project's dependencies:

```shell
$ mix deps.get
```

## Usage

Multitasks are processes meant to execute several functions asynchronously, and collect
their return values when they are done processing or the error of the first failure.

```elixir
iex> multitask = Multitask.async([
...>   fn -> {:ok, "a"} end,
...>   fn -> {:ok, "b"} end
...> ])
iex> Multitask.await(multitask)
{:ok, ["a", "b"]}
```

As shown in the example above, multitasks spawned with `async` can be awaited on by their
caller process (and only their caller). They are implemented by spawning a process that sends
a message to the caller once the given computation is performed. Internally, the multitask
process spawns a new process per function (linked and monitored by the multitask process) and
awaits for replies.

Functions must return `{:ok, result}` or `{:error, error}` tuples to express success or
failure. Multitasks implement a fail-fast behavior, reporting any failure or exit immediately
and shutting down all the processes.

```elixir
iex> multitask = Multitask.async([
...>   fn -> {:ok, "a"} end,
...>   fn -> {:ok, "b"} end,
...>   fn -> {:error, "c"} end
...> ])
iex> Multitask.await(multitask)
{:error, "c"}
```

Multitasks can also be started as part of a supervision tree and dynamically spawned on
remote nodes. [Check the documentation](https://hexdocs.pm/multitask) for more information.

## Documentation

Documentation is available at https://hexdocs.pm/multitask

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fertapric/multitask. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

### Running tests

Clone the repo and fetch its dependencies:

```shell
$ git clone https://github.com/fertapric/multitask.git
$ cd multitask
$ mix deps.get
$ mix test
```

### Building docs

```shell
$ mix docs
```

## License

**Multitask** is released under the [MIT License](https://opensource.org/licenses/MIT).

## Author

Fernando Tapia Rico, [@fertapric](https://twitter.com/fertapric)