You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+27-5Lines changed: 27 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -104,6 +104,14 @@ do e <- liftEff' myExcFunc
104
104
105
105
The `Aff` monad has error handling baked in, so ordinarily you don't have to worry about it.
106
106
107
+
When you need to deal with failure, you have several options.
108
+
109
+
1.**Attempt**
110
+
2.**Alt**
111
+
3.**MonadError**
112
+
113
+
### 1. Attempt
114
+
107
115
If you want to attempt a computation but recover from failure, you can use the `attempt` function:
108
116
109
117
```purescript
@@ -117,7 +125,7 @@ do e <- attempt $ Ajax.get "http://foo.com"
117
125
liftEff $ either (const $ trace "Oh noes!") (const $ trace "Yays!") e
118
126
```
119
127
120
-
### Alt
128
+
### 2. Alt
121
129
122
130
Because `Aff` has an `Alt` instance, you may also use the operator `<|>` to provide an alternative computation in the event of failure:
123
131
@@ -126,7 +134,7 @@ do result <- Ajax.get "http://foo.com" <|> Ajax.get "http://bar.com"
126
134
return result
127
135
```
128
136
129
-
### MonadError
137
+
### 3. MonadError
130
138
131
139
`Aff` has a `MonadError` instance, which comes with two functions: `catchError`, and `throwError`.
132
140
@@ -143,8 +151,8 @@ Thrown exceptions are propagated on the error channel, and can be recovered from
143
151
144
152
## Forking
145
153
146
-
Using the `forkAff`, you can "fork" an asynchronous computation, which means that its activities will not
147
-
block the current thread of execution:
154
+
Using the `forkAff`, you can "fork" an asynchronous computation, which means
155
+
that its activities will not block the current thread of execution:
148
156
149
157
```purescript
150
158
forkAff myAff
@@ -166,6 +174,20 @@ do v <- makeVar
166
174
liftEff $ trace ("Succeeded with " ++ show a)
167
175
```
168
176
169
-
# Documentation
177
+
## Parallel Execution
178
+
179
+
If you only need the power of `Applicative`, then instead of using the monadic `Aff`, you can use the `Par` newtype wrapper defined in `Control.Monad.Aff.Par`.
180
+
181
+
This provides parallel instances of `Apply` and `Alt`.
182
+
183
+
In the following example, two Ajax requests are initiated simultaneously (rather than in sequence, as they would be for `Aff`):
184
+
185
+
```purescript
186
+
runPar (f <$> Par (Ajax.get "http://foo.com") <*> Par (Ajax.get "http://foo.com"))
187
+
```
188
+
189
+
The `(<|>)` operator of the `Alt` instance of `Par` allows you to race two asynchronous computations, and use whichever value comes back first (or the first error, if both err).
0 commit comments