Skip to content

Commit f093554

Browse files
committed
add forkAff
1 parent 5018197 commit f093554

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

MODULES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ makeAff :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> EffA e Uni
6767
Creates an asynchronous effect from a function that accepts error and
6868
success callbacks.
6969

70+
#### `forkAff`
71+
72+
``` purescript
73+
forkAff :: forall e a. Aff e a -> Aff e Unit
74+
```
75+
76+
Forks the specified asynchronous computation so subsequent monadic binds
77+
will not block on the result of the computation.
78+
7079
#### `attempt`
7180

7281
``` purescript

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,19 @@ do response <- ajaxGet' req
7979
liftEff $ trace response.body
8080
```
8181

82-
## Converting from Eff
82+
## Eff
8383

84-
All purely synchronous computations (`Eff`) can be converted to `Aff` computations with `liftEff` defined in `Control.Monad.Eff.Class` (see [here](https://github.com/paf31/purescript-monad-eff)).
84+
All purely synchronous computations (`Eff`) can be lifted to asynchronous computations with `liftEff` defined in `Control.Monad.Eff.Class` (see [here](https://github.com/paf31/purescript-monad-eff)).
8585

8686
```purescript
8787
import Control.Monad.Eff.Class
8888
8989
liftEff $ trace "Hello world!"
9090
```
9191

92-
This lets you write your whole program in `Aff`, and still call out to synchronous `Eff` code.
92+
This lets you write your whole program in `Aff`, and still call out to synchronous code.
9393

94-
If your `Eff` code throws exceptions (`err :: Exception`), you can remove the exceptions using `liftEff'`
95-
to bring the exception to the value level as an `Either Error a`:
94+
If your `Eff` code throws exceptions (`err :: Exception`), you can remove the exceptions using `liftEff'`, which brings exceptions to the value level as an `Either Error a`:
9695

9796
```purescript
9897
do e <- liftEff' myExcFunc
@@ -140,6 +139,18 @@ do resp <- (Ajax.get "http://foo.com") `catchError` (\e -> pure defaultResponse)
140139

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

142+
## Variables
143+
144+
The `Control.Monad.Aff.Var` module contains asynchronous variables. These can
145+
be used as low-level building blocks for asynchronous programs.
146+
147+
```purescript
148+
do v <- makeVar
149+
forkAff (myRunner v)
150+
a <- takeVar
151+
return a
152+
```
153+
143154
# Documentation
144155

145156
[MODULES.md](MODULES.md)

src/Control/Monad/Aff.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Control.Monad.Aff
44
, EffA()
55
, PureAff(..)
66
, attempt
7+
, forkAff
78
, launchAff
89
, liftEff'
910
, makeAff
@@ -57,6 +58,26 @@ module Control.Monad.Aff
5758
makeAff :: forall e a. ((Error -> Eff e Unit) -> (a -> Eff e Unit) -> EffA e Unit) -> Aff e a
5859
makeAff = Aff
5960

61+
-- | Forks the specified asynchronous computation so subsequent monadic binds
62+
-- | will not block on the result of the computation.
63+
foreign import forkAff """
64+
function forkAff(aff) {
65+
return function(error) {
66+
return function(success) {
67+
return function() {
68+
try {
69+
aff(function(){})(function(){})();
70+
71+
success({})();
72+
} catch (e) {
73+
error(e)();
74+
}
75+
}
76+
}
77+
}
78+
}
79+
""" :: forall e a. Aff e a -> Aff e Unit
80+
6081
-- | Promotes any error to the value level of the asynchronous monad.
6182
attempt :: forall e a. Aff e a -> Aff e (Either Error a)
6283
attempt (Aff fa) = Aff (\_ f -> fa (Left >>> f) (Right >>> f))

0 commit comments

Comments
 (0)