Skip to content

Instantly share code, notes, and snippets.

@yamadapc

yamadapc/Main.hs Secret

Created December 14, 2015 14:06
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/6d75b4664f5845e38d26 to your computer and use it in GitHub Desktop.
Save yamadapc/6d75b4664f5845e38d26 to your computer and use it in GitHub Desktop.
import Control.Concurrent
import System.IO
fazRequestsHTTP mvar = do
putStrLn "[http] Fetching google.com"
threadDelay 1000
putStrLn "[http] Fetching google.com/something"
threadDelay 1000
putMVar mvar "<html>...</html>"
-- Todo programa em Haskell tem uma função main
main = do
hSetBuffering stdout LineBuffering
-- Criamos uma MVar vazia para a resposta
caixinhaParaAResposta <- newEmptyMVar
-- Começamos nosso thread
forkIO (fazRequestsHTTP caixinhaParaAResposta)
-- Essa linha vai ser impressa imediatamente
putStrLn "Eu apareço antes do callback ser executado!"
-- Esperamos até ter um valor na caixinha
valor <- takeMVar caixinhaParaAResposta
putStrLn ("valor: " ++ valor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment