Last active
April 5, 2024 11:44
-
-
Save DavideGalilei/34cff9dd13b6e8ac0db08b18336272fa to your computer and use it in GitHub Desktop.
Nim await future with timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std / asyncdispatch | |
type TimeoutError* = ref object of CatchableError | |
timeout*: int | |
proc timeoutFuture*[T](future: Future[T], timeout: int): Future[T] {.async.} = | |
let timeoutFuture = sleepAsync(timeout * 1000) | |
timeoutFuture.callback = proc = | |
if not future.finished: | |
future.fail(TimeoutError(msg: "The future didn't complete in time", timeout: timeout)) | |
return await future | |
when isMainModule: | |
proc longOperation(s: int): Future[int] {.async.} = | |
await sleepAsync(s * 1000) | |
return s | |
proc main {.async.} = | |
const TIMEOUT_SECONDS = 4 | |
let x = await timeoutFuture(longOperation(2), timeout = TIMEOUT_SECONDS) | |
echo "Got x value: ", x | |
let y = await timeoutFuture(longOperation(6), timeout = TIMEOUT_SECONDS) | |
echo "Got y value: ", y | |
waitFor main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment