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
+16-7Lines changed: 16 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -35,18 +35,18 @@ deleteBlankLines path =
35
35
saveFile path contents'
36
36
```
37
37
38
-
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).
38
+
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.
39
39
40
-
The library contains instances for `Semigroup`, `Monoid`, `Apply`, `Applicative`, `Bind`, `Monad`, `Alt`, `Plus`, `MonadPlus`, `MonadEff`, and `MonadError`. These instances allow you to compose `Aff`-ectful code as easily as `Eff`, as well as interop with existing `Eff` code.
40
+
The library contains instances for `Semigroup`, `Monoid`, `Apply`, `Applicative`, `Bind`, `Monad`, `Alt`, `Plus`, `MonadPlus`, `MonadEff`, and `MonadError`. These instances allow you to compose asynchronous code as easily as `Eff`, as well as interop with existing `Eff` code.
41
41
42
42
## Escaping Callback Hell
43
43
44
44
Hopefully, you're using libraries that already use the `Aff` type, so you don't even have to think about callbacks!
45
45
46
-
If you're building your own library, or you have to interact with some native code that expects callbacks, then *purescript-aff* provides a `makeAff` function you can use:
46
+
If you're building your own library, or you have to interact with some native code that expects callbacks, then *purescript-aff* provides a `makeAff` function:
47
47
48
48
```purescript
49
-
makeAff :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> EffA e Unit) -> Aff e a
49
+
makeAff :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> Eff e Unit) -> Aff e a
50
50
```
51
51
52
52
This function expects you to provide a handler, which should call a user-supplied error callback or success callback with the result of the asynchronous computation.
@@ -64,7 +64,7 @@ function ajaxGet(callback) { // accepts a callback
64
64
}
65
65
}
66
66
}
67
-
""" :: forall e. (Response -> Eff e Unit) -> Request -> EffA e Unit
67
+
""" :: forall e. (Response -> Eff e Unit) -> Request -> Eff e Unit
68
68
```
69
69
70
70
We can wrap this into an asynchronous computation like so:
@@ -142,7 +142,7 @@ These are defined in [purescript-transformers](http://github.com/purescript/pure
142
142
Here's an example of how you can use them:
143
143
144
144
```purescript
145
-
do resp <- (Ajax.get "http://foo.com") `catchError` (\e -> pure defaultResponse)
145
+
do resp <- (Ajax.get "http://foo.com") `catchError` (const $ pure defaultResponse)
146
146
if resp.statusCode != 200 then throwError myErr
147
147
else pure resp.body
148
148
```
@@ -162,6 +162,15 @@ Because Javascript is single-threaded, forking does not actually cause the
162
162
computation to be run in a separate thread. Forking just allows the subsequent
163
163
actions to execute without waiting for the forked computation to complete.
164
164
165
+
If the asynchronous computation supports it, you can "kill" a forked computation
166
+
using the returned canceler:
167
+
168
+
```purescript
169
+
canceler <- forkAff myAff
170
+
canceled <- canceler
171
+
_ <- liftEff $ if canceled then (trace "Canceled") else (trace "Not Canceled")
172
+
```
173
+
165
174
## Queues
166
175
167
176
The `Control.Monad.Aff.Queue` module contains asynchronous queues. These can
@@ -175,7 +184,7 @@ do v <- makeQueue
175
184
```
176
185
177
186
You can use these constructs as one-sided blocking queues, which suspend (if
178
-
necessary) on `take operations, or as asynchronous variables (similar to
187
+
necessary) on `take` operations, or as asynchronous variables (similar to
0 commit comments