Skip to content

Commit 4993b0c

Browse files
authored
async: add try-resolve (#45)
1 parent 6398eb3 commit 4993b0c

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

std/async/async.kk

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,20 @@ pub fun try-await( p : promise<a> ) : <async,ndet> maybe<a>
8888
// Resolve a promise to `value`. Raises an exception if the promise was already resolved.
8989
pub fun resolve( p : promise<a>, value : a ) : asyncx ()
9090
async-io
91-
val r = p.state
92-
match !r
93-
Awaiting(listeners) ->
94-
r := Resolved(value)
95-
listeners.foreach fn(cbx) // todo: through set-immediate?
96-
cbx(value) // set-immediate1( cbx, value )
97-
_ -> throw("Promise was already resolved")
91+
if !try-resolve-io(p, value) then
92+
throw("Promise was already resolved")
93+
94+
// Resolve a promise to `value`. Returns False if the promise was already resolved.
95+
// TODO: make this io-noexn
96+
pub fun try-resolve-io( p : promise<a>, value : a ) : io bool
97+
val r = p.state
98+
match !r
99+
Awaiting(listeners) ->
100+
r := Resolved(value)
101+
listeners.foreach fn(cbx) // todo: through set-immediate?
102+
cbx(value) // set-immediate1( cbx, value )
103+
True
104+
_ -> False
98105

99106
// ----------------------------------------------------------------------------
100107
// Channels

0 commit comments

Comments
 (0)