Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. Pauan revised this gist Mar 10, 2017. 1 changed file with 13 additions and 18 deletions.
    31 changes: 13 additions & 18 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -10,25 +10,20 @@ import Network.HTTP.Affjax as Ajax
    import Data.Either (Either(..), either)

    api1 = do
    e <- attempt $ Ajax.get "/api1"
    case e of
    Right resp -> pure (Right resp.response)
    Left error -> pure (Left "Error")

    api2 (Right x) = do
    resp <- Ajax.get "/api1"
    pure resp.response

    api2 x = do
    let url = "/api" <> x
    e <- attempt $ Ajax.get url

    case e of
    Right resp -> pure (Right resp.response)
    Left error -> pure (Left "Error")

    api2 (Left error) = pure (Left error)
    resp <- Ajax.get url
    pure resp.response

    main = launchAff do
    x <- api1
    y <- api2 x

    case y of
    result <- attempt do
    x <- api1
    y <- api2 x
    ...

    case result of
    Right resp -> log resp
    Left error -> log error
    Left error -> log error
  2. @knaman2609 knaman2609 created this gist Mar 8, 2017.
    34 changes: 34 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    module Main where

    import Prelude
    import Control.Monad.Eff (Eff)
    import Control.Monad.Eff.Class
    import Control.Monad.Aff (launchAff, Aff, makeAff, attempt)
    import Control.Monad.Aff.Console (CONSOLE, log)
    import Control.Monad.Eff.Exception (Error, try)
    import Network.HTTP.Affjax as Ajax
    import Data.Either (Either(..), either)

    api1 = do
    e <- attempt $ Ajax.get "/api1"
    case e of
    Right resp -> pure (Right resp.response)
    Left error -> pure (Left "Error")

    api2 (Right x) = do
    let url = "/api" <> x
    e <- attempt $ Ajax.get url

    case e of
    Right resp -> pure (Right resp.response)
    Left error -> pure (Left "Error")

    api2 (Left error) = pure (Left error)

    main = launchAff do
    x <- api1
    y <- api2 x

    case y of
    Right resp -> log resp
    Left error -> log error