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
60
60
e <- attempt $ takeVar v
61
61
either (const $ trace "Success : Killed queue dead ") (const $ trace "Failure : Oh noes , queue survived !") e
62
62
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
+
63
78
test_parRace :: TestAVar _
64
79
test_parRace = do
65
80
s <- runPar (Par (later' 100 $ pure "Success: Early bird got the worm") <|>
@@ -134,6 +149,9 @@ module Examples where
134
149
trace " Testing AVar killVar"
135
150
test_killVar
136
151
152
+ trace " Testing finally"
153
+ test_finally
154
+
137
155
trace " Testing Par (<|>)"
138
156
test_parRace
139
157
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ module Control.Monad.Aff
6
6
, attempt
7
7
, cancel
8
8
, cancelWith
9
+ , finally
9
10
, forkAff
10
11
, later
11
12
, later'
@@ -90,6 +91,13 @@ module Control.Monad.Aff
90
91
later' :: forall e a. Number -> Aff e a -> Aff e a
91
92
later' n aff = runFn3 _setTimeout nonCanceler n aff
92
93
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
+
93
101
-- | Forks the specified asynchronous computation so subsequent computations
94
102
-- | will not block on the result of the computation.
95
103
-- |
You can’t perform that action at this time.
0 commit comments