Fix asyncCheck.

This commit is contained in:
cheatfate 2018-06-15 20:05:43 +03:00
parent c102deac9d
commit d949710d8d
1 changed files with 5 additions and 13 deletions

View File

@ -358,25 +358,17 @@ proc failed*(future: FutureBase): bool =
## Determines whether ``future`` completed with an error. ## Determines whether ``future`` completed with an error.
return future.error != nil return future.error != nil
proc asyncCheckProxy[T](udata: pointer) =
var future = cast[Future[T]](udata)
if future.failed:
injectStacktrace(future)
raise future.error
proc asyncCheck*[T](future: Future[T]) = proc asyncCheck*[T](future: Future[T]) =
## Sets a callback on ``future`` which raises an exception if the future ## Sets a callback on ``future`` which raises an exception if the future
## finished with an error. ## finished with an error.
## ##
## This should be used instead of ``discard`` to discard void futures. ## This should be used instead of ``discard`` to discard void futures.
assert(not future.isNil, "Future is nil") assert(not future.isNil, "Future is nil")
# ZAH: This should probably add a callback instead of replacing all call-backs. proc cb(data: pointer) =
# Perhaps a new API can be introduced to avoid the breaking change. if future.failed:
future.callback = asyncCheckProxy[T] injectStacktrace(future)
# proc (udata: pointer) = raise future.error
# if future.failed: future.callback = cb
# injectStacktrace(future)
# raise future.error
# ZAH: The return type here could be a Future[(T, Y)] # ZAH: The return type here could be a Future[(T, Y)]
proc `and`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] = proc `and`*[T, Y](fut1: Future[T], fut2: Future[Y]): Future[void] =