Skip to content

Commit cca0510

Browse files
committed
update README
1 parent 2d98adc commit cca0510

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

README.md

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ do e <- liftEff' myExcFunc
104104

105105
The `Aff` monad has error handling baked in, so ordinarily you don't have to worry about it.
106106

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+
107115
If you want to attempt a computation but recover from failure, you can use the `attempt` function:
108116

109117
```purescript
@@ -117,7 +125,7 @@ do e <- attempt $ Ajax.get "http://foo.com"
117125
liftEff $ either (const $ trace "Oh noes!") (const $ trace "Yays!") e
118126
```
119127

120-
### Alt
128+
### 2. Alt
121129

122130
Because `Aff` has an `Alt` instance, you may also use the operator `<|>` to provide an alternative computation in the event of failure:
123131

@@ -126,7 +134,7 @@ do result <- Ajax.get "http://foo.com" <|> Ajax.get "http://bar.com"
126134
return result
127135
```
128136

129-
### MonadError
137+
### 3. MonadError
130138

131139
`Aff` has a `MonadError` instance, which comes with two functions: `catchError`, and `throwError`.
132140

@@ -143,8 +151,8 @@ Thrown exceptions are propagated on the error channel, and can be recovered from
143151

144152
## Forking
145153

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:
148156

149157
```purescript
150158
forkAff myAff
@@ -166,6 +174,20 @@ do v <- makeVar
166174
liftEff $ trace ("Succeeded with " ++ show a)
167175
```
168176

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).
190+
191+
# API Docs
170192

171193
[MODULES.md](MODULES.md)

0 commit comments

Comments
 (0)