Created
          December 27, 2016 22:33 
        
      - 
      
- 
        Save mmartinson/79ac288f4233202f2e7c12d9a4a7a397 to your computer and use it in GitHub Desktop. 
    Elixir Task async/await example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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