Skip to content

Instantly share code, notes, and snippets.

@visar
Forked from tomku/sleep_example.exs
Created July 20, 2016 14:01
Show Gist options
  • Save visar/3074eb220dc1c97ba912fff72992a6f7 to your computer and use it in GitHub Desktop.
Save visar/3074eb220dc1c97ba912fff72992a6f7 to your computer and use it in GitHub Desktop.
urls = ["http://www.google.com", "http://www.bing.com", "http://www.yahoo.com"]
for_tasks = for {url, i} <- Enum.with_index(urls) do
Task.async(fn ->
:timer.sleep(3000 * i)
IO.puts("for ##{i}: #{url}")
end)
end
map_tasks = urls
|> Enum.with_index
|> Enum.map(fn({url, i}) ->
Task.async(fn ->
:timer.sleep(9000 + 3000 * i)
IO.puts("map ##{i}: #{url}")
end)
end)
Task.yield_many(Enum.concat(for_tasks, map_tasks), 20000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment