snowhite v2.1.3 Snowhite.Helpers.TaskRunner View Source

Link to this section Summary

Functions

Runs multiple tasks concurrently and returns once they all done.

Link to this section Types

Specs

option() :: {:map_after, function()}
Link to this type

task_definition(key_type)

View Source

Specs

task_definition(key_type) ::
  {key_type, function()} | {key_type, function(), [any()]}

Link to this section Functions

Link to this function

run(tasks, options \\ [])

View Source

Specs

run([task_definition(any())], [option()]) :: [{any(), any()}]

Runs multiple tasks concurrently and returns once they all done.

Following options are supported:

  • map_after (function, fallbacks to identity function): Runs the function on the await's result to map over the result of the task.

Examples

iex> alias Snowhite.Helpers.TaskRunner
iex> TaskRunner.run([
  {:first, fn ->
    :timer.sleep(1000)
    {:done, 1000}
  end},
  {:first, fn ms ->
    :timer.sleep(ms)
    {:done, ms}
  end, [2000]}
])
[{:first, {:done, 1000}}, {:second, {:done, 2000}}] # after 2000ms

iex> map_after = fn v -> v / 1000 end
iex> TaskRunner.run([
  {:first, fn ->
    :timer.sleep(1000)
    {:done, 1000}
  end},
  {:first, fn ms ->
    :timer.sleep(ms)
    {:done, ms}
  end, [2000]}
], map_after)
[{:first, {:done, 1}}, {:second, {:done, 2}}] # after 2000ms