File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,21 @@ module Examples where
6060 e <- attempt $ takeVar v
6161 either (const $ trace "Success : Killed queue dead ") (const $ trace "Failure : Oh noes , queue survived !") e
6262
63+ test_finally :: TestAVar _
64+ test_finally = do
65+ v <- makeVar
66+ finally
67+ (putVar v 0)
68+ (putVar v 2)
69+ apathize $ finally
70+ (throwError (error "poof!") *> putVar v 666) -- this putVar should not get executed
71+ (putVar v 40)
72+ n1 <- takeVar v
73+ n2 <- takeVar v
74+ n3 <- takeVar v
75+ trace $ if n1 + n2 + n3 == 42 then " Success: effects amount to 42."
76+ else " Failure: Expected 42."
77+
6378 test_parRace :: TestAVar _
6479 test_parRace = do
6580 s <- runPar (Par (later' 100 $ pure "Success: Early bird got the worm") <|>
@@ -134,6 +149,9 @@ module Examples where
134149 trace " Testing AVar killVar"
135150 test_killVar
136151
152+ trace " Testing finally"
153+ test_finally
154+
137155 trace " Testing Par (<|>)"
138156 test_parRace
139157
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ module Control.Monad.Aff
66 , attempt
77 , cancel
88 , cancelWith
9+ , finally
910 , forkAff
1011 , later
1112 , later'
@@ -90,6 +91,13 @@ module Control.Monad.Aff
9091 later' :: forall e a. Number -> Aff e a -> Aff e a
9192 later' n aff = runFn3 _setTimeout nonCanceler n aff
9293
94+ -- | Compute `aff1`, followed by `aff2` regardless of whether `aff1` terminated successfully.
95+ finally :: forall e a b. Aff e a -> Aff e b -> Aff e a
96+ finally aff1 aff2 = do
97+ x <- attempt aff1
98+ aff2
99+ either throwError pure x
100+
93101 -- | Forks the specified asynchronous computation so subsequent computations
94102 -- | will not block on the result of the computation.
95103 -- |
You can’t perform that action at this time.
0 commit comments