Skip to content

Instantly share code, notes, and snippets.

@mmartinson
Created December 27, 2016 22:33
Show Gist options
  • Save mmartinson/79ac288f4233202f2e7c12d9a4a7a397 to your computer and use it in GitHub Desktop.
Save mmartinson/79ac288f4233202f2e7c12d9a4a7a397 to your computer and use it in GitHub Desktop.
Elixir Task async/await example
t = [1,1,2,2,3]
f = fn(n) ->
IO.puts("starting #{n}")
:timer.sleep(n * 1000)
IO.puts("finishing #{n}")
n
end
tasks = Enum.map(t, fn(n) ->
Task.async(fn -> f.(n) end)
end)
:timer.sleep(6000)
Enum.each(tasks, fn(task) ->
Task.await(task) |> IO.inspect
end
)
IO.puts "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment