Add a helper from NBC

This commit is contained in:
Zahary Karadjov 2020-06-17 19:14:05 +03:00 committed by zah
parent 964a380191
commit 5c6d8ccb36
1 changed files with 17 additions and 0 deletions

View File

@ -18,3 +18,20 @@ template traceAwaitErrors*(fut: FutureBase) =
yield f
if not f.error.isNil:
catchOrQuit f.error[]
template awaitWithTimeout*[T](operation: Future[T],
deadline: Future[void],
onTimeout: untyped): T =
let f = operation
await f or deadline
if not f.finished:
cancel f
onTimeout
else:
f.read
template awaitWithTimeout*[T](operation: Future[T],
timeout: Duration,
onTimeout: untyped): T =
awaitWithTimeout(operation, sleepAsync(timeout), onTimeout)