@@ -37,17 +37,17 @@ asynchronously without any callbacks. Error handling is baked in so you only
3737deal with it when you want to.
3838
3939The 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.
4343
4444## Escaping Callback Hell
4545
4646Hopefully, you're using libraries that already use the ` Aff ` type, so you
4747don't even have to think about callbacks!
4848
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 ` .
5151
5252``` purescript
5353makeAff :: 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
6060` Aff ` threads may be killed, all asynchronous operations should provide a
6161mechanism for unscheduling it.
6262
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:
6565
6666``` javascript
6767exports ._ajaxGet = function (request ) { // accepts a request
@@ -108,12 +108,9 @@ example = do
108108## Eff
109109
110110All 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 ` .
113112
114113``` purescript
115- import Control.Monad.Eff.Class
116-
117114liftEff $ log "Hello world!"
118115```
119116
@@ -212,18 +209,47 @@ example = do
212209## AVars
213210
214211The ` 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 ` .
217216
218217``` 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
221223 _ <- 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
226227```
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.
227253
228254## Parallel Execution
229255
0 commit comments