@@ -37,17 +37,17 @@ asynchronously without any callbacks. Error handling is baked in so you only
37
37
deal with it when you want to.
38
38
39
39
The library contains instances for ` Semigroup ` , ` Monoid ` , ` Apply ` ,
40
- ` Applicative ` , ` Bind ` , ` Monad ` , ` Alt ` , ` Plus ` , ` MonadEff ` , and ` MonadError ` .
41
- These instances allow you to compose asynchronous code as easily as ` Eff ` , as
42
- well as interop with existing ` Eff ` code.
40
+ ` Applicative ` , ` Bind ` , ` Monad ` , ` Alt ` , ` Plus ` , ` MonadEff ` , ` MonadError ` , and
41
+ ` Parallel ` . These instances allow you to compose asynchronous code as easily
42
+ as ` Eff ` , as well as interop with existing ` Eff ` code.
43
43
44
44
## Escaping Callback Hell
45
45
46
46
Hopefully, you're using libraries that already use the ` Aff ` type, so you
47
47
don't even have to think about callbacks!
48
48
49
- If you're building your own library, then * purescript-aff * provides a
50
- ` makeAff ` function:
49
+ If you're building your own library, then you can make an ` Aff ` from
50
+ low-level ` Eff ` callbacks with ` makeAff ` .
51
51
52
52
``` purescript
53
53
makeAff :: forall eff a. ((Either Error a -> Eff eff Unit) -> Eff eff (Canceler eff)) -> Aff eff a
@@ -60,8 +60,8 @@ You should also return `Canceler`, which is just a cleanup effect. Since
60
60
` Aff ` threads may be killed, all asynchronous operations should provide a
61
61
mechanism for unscheduling it.
62
62
63
- * purescript-aff * also provides functions for easily binding FFI definitions in
64
- ` Control.Monad.Aff.Compat ` .
63
+ ` Control.Monad.Aff.Compat ` provides functions for easily binding FFI
64
+ definitions:
65
65
66
66
``` javascript
67
67
exports ._ajaxGet = function (request ) { // accepts a request
@@ -108,12 +108,9 @@ example = do
108
108
## Eff
109
109
110
110
All purely synchronous computations (` Eff ` ) can be lifted to asynchronous
111
- computations with ` liftEff ` defined in ` Control.Monad.Eff.Class ` (see
112
- [ here] ( https://github.com/purescript/purescript-eff ) ).
111
+ computations with ` liftEff ` defined in ` Control.Monad.Eff.Class ` .
113
112
114
113
``` purescript
115
- import Control.Monad.Eff.Class
116
-
117
114
liftEff $ log "Hello world!"
118
115
```
119
116
@@ -212,18 +209,47 @@ example = do
212
209
## AVars
213
210
214
211
The ` Control.Monad.Aff.AVar ` module contains asynchronous variables, which
215
- are very similar to Haskell's ` MVar ` . These can be used as low-level building
216
- blocks for asynchronous programs.
212
+ are very similar to Haskell's ` MVar ` .
213
+
214
+ ` AVar ` s represent a value that is either full or empty. Calling ` takeVar ` on
215
+ an empty ` AVar ` will queue until it is filled by a matching ` putVar ` .
217
216
218
217
``` purescript
219
- example = d
220
- v <- makeEmptyVar
218
+ example = do
219
+ var <- makeEmptyVar
220
+ _ <- forkAff do
221
+ value <- takeVar var
222
+ log $ "Got a value: " <> value
221
223
_ <- forkAff do
222
- delay (Milliseconds 50.0)
223
- putVar v 1.0
224
- a <- takeVar v
225
- log ("Succeeded with " <> show a)
224
+ delay (Milliseconds 100.0)
225
+ putVar var "hello"
226
+ pure unit
226
227
```
228
+ ```
229
+ (Waits 100ms)
230
+ > Got a value: hello
231
+ ```
232
+
233
+ Likewise, calling ` putVar ` will queue until it is taken:
234
+
235
+ ``` purescript
236
+ example = do
237
+ var <- makeEmptyVar
238
+ _ <- forkAff do
239
+ delay (Milliseconds 100.0)
240
+ value <- takeVar var
241
+ log $ "Got a value: " <> value
242
+ putVar var "hello"
243
+ log "Value taken"
244
+ ```
245
+ ```
246
+ (Waits 100ms)
247
+ > Value taken
248
+ > Got a value: hello
249
+ ```
250
+
251
+ These combinators (and a few more) can be used as the building blocks for
252
+ complex asynchronous coordination.
227
253
228
254
## Parallel Execution
229
255
0 commit comments