Module future

A Future represents the result of an asynchronous computation. Completer is used to create and complete Futures - it can be thought as of an other side of a Future.

Types

Future*[T] = object {.
requiresinit
.} case isImmediate: bool of true: value: T of false: completer: Completer[T]
  Source Edit
Completer*[T] = ref object of RootObj
  when debugFutures:
      stackTrace: string

  consumed: bool
  case isFinished: bool
  of true:
      result: Result[T]

  of false:
      callback: FutureCallback[T]
      callbackList: CallbackList[T]

  
  Source Edit
Bottom* = object
  Source Edit

Procs

proc `$`*[T](c: Future[T]): string
  Source Edit
proc `$`*[T](c: Completer[T]): string
  Source Edit
proc getFuture*[T](c: Completer[T]): Future[T]
Retrieves a Future managed by the Completer.   Source Edit
proc internalGetCompleter*[T](c: Future[T]): Completer[T]
Retrieves a Completer behind this Future. Returns nil is the future is immediate. Only for low-level use.   Source Edit
proc newCompleter*[T](): Completer[T]
Creates a new completer.   Source Edit
proc immediateFuture*[T](value: T): Future[T] {.
deprecated
.}
  Source Edit
proc immediateFuture*(): Future[void] {.
deprecated
.}
  Source Edit
proc immediateError*[T](value: string): Future[T] {.
deprecated
.}
  Source Edit
proc immediateError*[T](value: ref Exception): Future[T] {.
deprecated
.}
  Source Edit
proc isCompleted*(self: Future): bool
Checks if a Future is completed.   Source Edit
proc isSuccess*(self: Future): bool
Checks if a Future is completed and doesn't contain an error.   Source Edit
proc getResult*[T](self: Future[T]): Result[T]
Returns the result represented by a completed Future.   Source Edit
proc get*[T](self: Future[T]): T
Returns the value represented by a completed Future. If the Future contains an error, raise it as an exception.   Source Edit
proc completeResult*[T](self: Completer[T]; x: Result[T])
Complete a Future managed by the Completer with result x.   Source Edit
proc complete*[T](self: Completer[T]; x: T)
Complete a Future managed by the Completer with value x.   Source Edit
proc complete*(self: Completer[void])
Complete a void Future managed by the Completer.   Source Edit
proc completeError*[T](self: Completer[T]; x: ref Exception)
Complete a Future managed by the Completer with error x.   Source Edit
proc onSuccessOrError*[T](f: Future[T]; onSuccess: (proc (t: T));
                         onError: (proc (t: ref Exception)))
Call onSuccess or onError when Future is completed. If Future is already completed, one of these functions is called immediately.   Source Edit
proc onSuccessOrError*(f: Future[void]; onSuccess: (proc ());
                      onError: (proc (t: ref Exception)))
  Source Edit
proc onSuccessOrError*(f: Future[void]; function: (proc (t: Result[void])))
  Source Edit
proc onSuccessOrError*[T](f: Future[T]; function: (proc (t: Result[T])))
  Source Edit
proc onError*(f: Future[Bottom]; onError: (proc (t: ref Exception)))
  Source Edit
proc ignoreResult*[T](f: Future[T]): Future[Bottom]
  Source Edit
proc ignoreResultValue*[T](f: Future[T]): Future[void]
  Source Edit
proc ignoreError*[Exc](f: Future[void]; kind: typedesc[Exc]): Future[void]
Ignore an error in Future f of kind kind and transform it into successful completion.   Source Edit
proc completeFrom*[T](c: Completer[T]; f: Future[T])
When Future f completes, complete the Future managed by c with the same result.   Source Edit
proc complete*[T](c: Completer[T]; f: Future[T])
alias for completeFrom   Source Edit
proc then*[T](f: Future[void]; function: (proc (): T)): auto
  Source Edit
proc then*[T](f: Future[T]; function: (proc (t: T))): auto
  Source Edit
proc then*(f: Future[void]; function: (proc ())): auto
  Source Edit
proc then*[T, R](f: Future[T]; function: (proc (t: T): R)): auto
  Source Edit
proc ignore*(f: Future[void])
Discard the future result.   Source Edit
proc ignore*[T](f: Future[T])
Discard the future result.   Source Edit
proc completeError*(self: Completer; x: string)
  Source Edit
proc waitForever*(): Future[void]
Returns a future that never completes.   Source Edit
proc waitForever*[T](t: typedesc[T]): Future[T]
Returns a future that never completes.   Source Edit
proc `or`*(a: Future[void]; b: Future[void]): Future[void]
  Source Edit
proc runLoop*[T](f: Future[T]): T
Run the event loop until Future f completes, return the value. If the Future completes with an error, raise it as an exception. Consider using runMain instead of this.   Source Edit
proc waitFor*[T](f: Future[T]): T
  Source Edit
proc runMain*(f: Future[void])
Run the event loop until Future f completes, return the value. If the Future completes with an error, print pretty stack trace and quit.   Source Edit

Converters

converter now*[T](res: Result[T]): Future[T]
Returns already completed Future containing result res.   Source Edit
converter ignoreVoidResult*(f: Future[void]): Future[Bottom] {.
deprecated
.}
  Source Edit