Skip to content

Commit 3faa8a3

Browse files
committed
Merge pull request #33 from hdgarrood/rethrow-in-launch
Stop ignoring exceptions in `launchAff`
2 parents bcd84de + 2dc3c8c commit 3faa8a3

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

docs/Control.Monad.Aff.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,19 @@ will be run along side the computation's own canceler.
7979
#### `launchAff`
8080

8181
``` purescript
82-
launchAff :: forall e a. Aff e a -> Eff e Unit
82+
launchAff :: forall e a. Aff e a -> Eff (err :: EXCEPTION | e) Unit
8383
```
8484

8585
Converts the asynchronous computation into a synchronous one. All values
86-
and errors are ignored.
86+
are ignored, and if the computation produces an error, it is thrown.
87+
88+
Catching exceptions by using `catchException` with the resulting Eff
89+
computation is not recommended, as exceptions may end up being thrown
90+
asynchronously, in which case they cannot be caught.
91+
92+
If you do need to handle exceptions, you can use `runAff` instead, or
93+
you can handle the exception within the Aff computation, using
94+
`catchError` (or any of the other mechanisms).
8795

8896
#### `runAff`
8997

src/Control/Monad/Aff.purs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Control.Monad.Aff
2626
import Control.Monad.Cont.Class(MonadCont)
2727
import Control.Monad.Eff(Eff())
2828
import Control.Monad.Eff.Class(MonadEff, liftEff)
29-
import Control.Monad.Eff.Exception(Error(), EXCEPTION(), catchException, error)
29+
import Control.Monad.Eff.Exception(Error(), EXCEPTION(), catchException, throwException, error)
3030
import Control.Monad.Eff.Unsafe(unsafeInterleaveEff)
3131
import Control.Monad.Error.Class(MonadError, throwError)
3232
import Control.Monad.Rec.Class(MonadRec, tailRecM)
@@ -65,9 +65,20 @@ module Control.Monad.Aff
6565
cancelWith aff c = runFn3 _cancelWith nonCanceler aff c
6666

6767
-- | Converts the asynchronous computation into a synchronous one. All values
68-
-- | and errors are ignored.
69-
launchAff :: forall e a. Aff e a -> Eff e Unit
70-
launchAff = runAff (const (pure unit)) (const (pure unit))
68+
-- | are ignored, and if the computation produces an error, it is thrown.
69+
-- |
70+
-- | Catching exceptions by using `catchException` with the resulting Eff
71+
-- | computation is not recommended, as exceptions may end up being thrown
72+
-- | asynchronously, in which case they cannot be caught.
73+
-- |
74+
-- | If you do need to handle exceptions, you can use `runAff` instead, or
75+
-- | you can handle the exception within the Aff computation, using
76+
-- | `catchError` (or any of the other mechanisms).
77+
launchAff :: forall e a. Aff e a -> Eff (err :: EXCEPTION | e) Unit
78+
launchAff = runAff throwException (const (pure unit)) <<< liftEx
79+
where
80+
liftEx :: forall e a. Aff e a -> Aff (err :: EXCEPTION | e) a
81+
liftEx = _unsafeInterleaveAff
7182

7283
-- | Runs the asynchronous computation. You must supply an error callback and a
7384
-- | success callback.

0 commit comments

Comments
 (0)