|
2 | 2 |
|
3 | 3 | An asynchronous effect monad for PureScript. |
4 | 4 |
|
5 | | -The moral equivalent of `ErrorT (ContT Unit (Eff (async :: Async e1 | e2)) a`, for synchronous effects `e1`, asynchronous effects `e2`, and value `a`. |
| 5 | +The moral equivalent of `ErrorT (ContT Unit (Eff (async :: Async e1 | e2)) a`, for synchronous effects `e2`, asynchronous effects `e1`, and value `a`. |
6 | 6 |
|
7 | 7 | `Aff` lets you say goodbyte to monad transformers and callback hell! |
8 | 8 |
|
@@ -41,7 +41,7 @@ For maximum type safety, `Aff` separates the effects of the synchronous part fro |
41 | 41 |
|
42 | 42 | Asynchronous effects are represented with an `async :: Async e1` effect label, where `e1` is the row of effects for actions to occur at some point in the future. |
43 | 43 |
|
44 | | -The library contains instances for `Semigroup`, `Monoid`, `Apply`, `Applicative`, `Bind`, `Monad`, and `MonadEff`. These instances allow you to compose `Aff`-ectful code as easily as `Eff`, as well as interop with existing `Eff` code. |
| 44 | +The library contains instances for `Semigroup`, `Monoid`, `Apply`, `Applicative`, `Bind`, `Monad`, `MonadEff`. and `MonadError`. These instances allow you to compose `Aff`-ectful code as easily as `Eff`, as well as interop with existing `Eff` code. |
45 | 45 |
|
46 | 46 | ## Escaping Callback Hell |
47 | 47 |
|
@@ -109,15 +109,18 @@ attempt :: forall e1 e2 a. Aff e1 e2 a -> Aff e1 e2 (Either Error a) |
109 | 109 |
|
110 | 110 | This returns an `Either Error a` you can use to recover gracefully from failure. |
111 | 111 |
|
112 | | -The "opposite" of `attempt` is `throw`, which allows you to "throw" an exception: |
| 112 | +`Aff` has a `MonadError` instance, which comes with two functions: `catchError`, and `throwError`. |
| 113 | + |
| 114 | +These are defined in [purescript-transformers](http://github.com/purescript/purescript-transformers). |
| 115 | +Here's an example of how you can use them: |
113 | 116 |
|
114 | 117 | ```purescript |
115 | | -do resp <- Ajax.get "http://foo.com" |
116 | | - if resp.statusCode != 200 then throw myErr |
| 118 | +do resp <- (Ajax.get "http://foo.com") `catchError` (\e -> pure defaultResponse) |
| 119 | + if resp.statusCode != 200 then throwError myErr |
117 | 120 | else pure resp.body |
118 | 121 | ``` |
119 | 122 |
|
120 | | -Thrown exceptions are propagated on the error channel, and can be recovered from using `attempt`. |
| 123 | +Thrown exceptions are propagated on the error channel, and can be recovered from using `attempt` or `catchError`. |
121 | 124 |
|
122 | 125 | # Documentation |
123 | 126 |
|
|
0 commit comments