Skip to content

Instantly share code, notes, and snippets.

@tomku
Created July 11, 2016 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomku/21eab22e98950f31a072a2ac63900b16 to your computer and use it in GitHub Desktop.
Save tomku/21eab22e98950f31a072a2ac63900b16 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