Skip to content

Commit e73f9f5

Browse files
committed
Update docs
1 parent 5e3cf42 commit e73f9f5

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

README.md

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66

77
An asynchronous effect monad for PureScript.
88

9-
The moral equivalent of `ErrorT (ContT Unit (Eff e) a`, for effects `e`.
9+
The moral equivalent of `ErrorT (ContT Unit (Eff e)) a`, for effects `e`.
1010

1111
`Aff` lets you say goodbye to monad transformers and callback hell!
1212

1313
# Example
1414

1515
```purescript
16-
main = launchAff $
17-
do response <- Ajax.get "http://foo.bar"
18-
liftEff $ log response.body
16+
main = launchAff do
17+
response <- Ajax.get "http://foo.bar"
18+
liftEff $ log response.body
1919
```
2020

2121
See the [tests](https://github.com/slamdata/purescript-aff/blob/master/test/Test/Main.purs) for more examples.
@@ -33,10 +33,10 @@ bower install purescript-aff
3333
An example of `Aff` is shown below:
3434

3535
```purescript
36-
deleteBlankLines path =
37-
do contents <- loadFile path
38-
let contents' = S.join "\n" $ A.filter (\a -> S.length a > 0) (S.split "\n" contents)
39-
saveFile path contents'
36+
deleteBlankLines path = do
37+
contents <- loadFile path
38+
let contents' = S.join "\n" $ A.filter (\a -> S.length a > 0) (S.split "\n" contents)
39+
saveFile path contents'
4040
```
4141

4242
This looks like ordinary, synchronous, imperative code, but actually operates asynchronously without any callbacks. Error handling is baked in so you only deal with it when you want to.
@@ -149,23 +149,23 @@ Here's an example of how you can use them:
149149

150150
```purescript
151151
do resp <- (Ajax.get "http://foo.com") `catchError` (const $ pure defaultResponse)
152-
if resp.statusCode != 200 then throwError myErr
152+
if resp.statusCode != 200 then throwError myErr
153153
else pure resp.body
154154
```
155155

156156
Thrown exceptions are propagated on the error channel, and can be recovered from using `attempt` or `catchError`.
157157

158158
## Forking
159159

160-
Using the `forkAff`, you can "fork" an asynchronous computation, which means
160+
Using the `forkAff`, you can "fork" an asynchronous computation, which means
161161
that its activities will not block the current thread of execution:
162162

163163
```purescript
164164
forkAff myAff
165165
```
166166

167-
Because Javascript is single-threaded, forking does not actually cause the
168-
computation to be run in a separate thread. Forking just allows the subsequent
167+
Because Javascript is single-threaded, forking does not actually cause the
168+
computation to be run in a separate thread. Forking just allows the subsequent
169169
actions to execute without waiting for the forked computation to complete.
170170

171171
If the asynchronous computation supports it, you can "kill" a forked computation
@@ -191,28 +191,34 @@ The `Control.Monad.Aff.AVar` module contains asynchronous variables, which are v
191191
```purescript
192192
do v <- makeVar
193193
forkAff (later $ putVar v 1.0)
194-
a <- takeVar v
194+
a <- takeVar v
195195
liftEff $ log ("Succeeded with " ++ show a)
196196
```
197197

198-
You can use these constructs as one-sided blocking queues, which suspend (if
198+
You can use these constructs as one-sided blocking queues, which suspend (if
199199
necessary) on `take` operations, or as asynchronous, empty-or-full variables.
200200

201201
## Parallel Execution
202202

203-
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`.
203+
There are `MonadPar` and `MonadRace` instances defined for `Aff`, allowing for parallel execution of `Aff` computations.
204204

205-
This provides parallel instances of `Apply` and `Alt`.
205+
There are two ways of taking advantage of these instances - directly through the `par` and `race` functions from these classes, or by using the `Parallel` newtype wrapper that enables parallel behaviours through the `Applicative` and `Alternative` operators.
206206

207-
In the following example, two Ajax requests are initiated simultaneously (rather than in sequence, as they would be for `Aff`):
207+
In the following example, using the newtype, two Ajax requests are initiated simultaneously (rather than in sequence, as they would be for `Aff`):
208208

209209
```purescript
210-
runPar (f <$> Par (Ajax.get "http://foo.com") <*> Par (Ajax.get "http://foo.com"))
210+
runParallel (f <$> parallel (Ajax.get "http://foo.com") <*> parallel (Ajax.get "http://foo.com"))
211211
```
212212

213-
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).
213+
And the equivalent using the `MonadPar` function directly:
214214

215-
The `runPar` function allows you to unwrap the `Aff` and return to normal monadic (sequential) composition.
215+
```purescript
216+
par f (Ajax.get "http://foo.com") (Ajax.get "http://foo.com")
217+
```
218+
219+
The `race` function from `MonadPar` or the `(<|>)` operator of the `Alt` instance of `Parallel` allows you to race two asynchronous computations, and use whichever value comes back first (or the first error, if both err).
220+
221+
The `runParallel` function allows you to unwrap the `Aff` and return to normal monadic (sequential) composition.
216222

217223
A parallel computation can be canceled if both of its individual components can be canceled.
218224

0 commit comments

Comments
 (0)