mirror of https://github.com/status-im/nim-eth.git
awaitWithTimeout for Future[void]
This commit is contained in:
parent
a339944bcf
commit
e8c9691b35
|
@ -22,3 +22,21 @@ template awaitWithTimeout*[T](operation: Future[T],
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
onTimeout: untyped): T =
|
onTimeout: untyped): T =
|
||||||
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|
||||||
|
|
||||||
|
template awaitWithTimeout*(operation: Future[void],
|
||||||
|
deadline: Future[void],
|
||||||
|
onTimeout: untyped) =
|
||||||
|
let f = operation
|
||||||
|
await f or deadline
|
||||||
|
if not f.finished:
|
||||||
|
# If we don't wait for for the cancellation here, it's possible that
|
||||||
|
# the "next" operation will run concurrently to this one, messing up
|
||||||
|
# the order of operations (since await/async is not fair)
|
||||||
|
await cancelAndWait(f)
|
||||||
|
onTimeout
|
||||||
|
|
||||||
|
template awaitWithTimeout*(operation: Future[void],
|
||||||
|
timeout: Duration,
|
||||||
|
onTimeout: untyped) =
|
||||||
|
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue