Last active
February 14, 2017 01:28
-
-
Save soriyath/13e66361c5d4c9b4f593f2058663dc6a to your computer and use it in GitHub Desktop.
How to return value from Task.async?
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
defmodule Test do | |
@doc ~S""" | |
iex> Test.download("http://www.six-swiss-exchange.com/data/market/statistics/corrections_2017-02-03.csv", "test.csv") | |
""" | |
def download(src, output_filename) do | |
Logger.info "Downloading #{src} -> #{output_filename}" | |
Task.async(getURL(src)) | |
|> Task.await | |
|> Task.async(&(fn -> File.write!(output_filename, &1) end)) | |
|> Task.await | |
Logger.info("Done Downloading #{src} -> #{output_filename}") | |
end | |
defp getURL(src) do | |
case HTTPoison.get(src) do | |
{:ok, response} -> response.body | |
{:error, error} -> ( | |
Logger.error "could not download file. #{error.id} - #{error.reason}" | |
:error | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@soriyath Could you explain your exact goal in this gist?