Skip to content

Instantly share code, notes, and snippets.

@luite
Created May 27, 2015 16:33
Show Gist options
  • Select an option

  • Save luite/85f4c56f24412d363be6 to your computer and use it in GitHub Desktop.

Select an option

Save luite/85f4c56f24412d363be6 to your computer and use it in GitHub Desktop.
XHR with interruptible FFI
{-# LANGUAGE JavaScriptFFI #-}
module XhrB (xhr) where
import GHCJS.Types
import Control.Exception (onException)
data XHR_
type XHR = JSRef XHR_
foreign import javascript unsafe
"new XMLHttpRequest()"
js_createXHR :: IO XHR
foreign import javascript unsafe
"$1.abort();"
js_abortXHR :: IO ()
foreign import javascript interruptible
"$2.open('GET', $1);\
\$2.onload = function() { $c($2.responseText); };\
\$2.send();"
js_sendXHR :: JSString -> XHR -> IO JSString
xhr :: JSString -> IO JSString
xhr url = do
x <- js_createXHR
js_sendXHR url x `onException` js_abortXHR x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment