Skip to content

Instantly share code, notes, and snippets.

@yamadapc

yamadapc/Main.hs Secret

Created December 14, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamadapc/94afb7e8e2c92f822b1d to your computer and use it in GitHub Desktop.
Save yamadapc/94afb7e8e2c92f822b1d to your computer and use it in GitHub Desktop.
#!/usr/bin/env stack
-- stack runghc --package async
import Control.Concurrent
import Control.Concurrent.Async
import System.IO
fazRequestsHTTP = do
putStrLn "[http] Fetching google.com"
threadDelay 1000
putStrLn "[http] Fetching google.com/something"
threadDelay 1000
-- Retorna o resultado
return "<html>...</html>"
-- Todo programa em Haskell tem uma função main
main = do
hSetBuffering stdout LineBuffering
-- Começamos nosso thread
promessaParaResposta <- async fazRequestsHTTP
-- Essa linha vai ser impressa imediatamente
putStrLn "Eu apareço antes do callback ser executado!"
-- Esperamos pelo valor da promessa
valor <- wait promessaParaResposta
putStrLn ("valor: " ++ valor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment