On this page
asyncfutures
Imports
Types
- 
    
FutureBase = ref object of RootObj callbacks: CallbackList finished: bool error*: ref Exception ## Stored exception errorStackTrace*: string when not false: stackTrace: seq[StackTraceEntry] ## For debugging purposes only. id: int fromProc: string - Untyped future. Source Edit
 - 
    
Future[T] = ref object of FutureBase value: T ## Stored value - Typed future. Source Edit
 - 
    
FutureVar[T] = distinct Future[T] - Source Edit
 - 
    
FutureError = object of Defect cause*: FutureBase - Source Edit
 
Consts
- 
    
isFutureLoggingEnabled = false - Source Edit
 - 
    
NimAsyncContinueSuffix = "NimAsyncContinue" - For internal usage. Do not use. Source Edit
 
Procs
- 
    
proc getCallSoonProc(): (proc (cbproc: proc ()) {...}{.gcsafe.}) {...}{.raises: [], tags: [].} - 
    Get current implementation of 
callSoon. Source Edit - 
    
proc setCallSoonProc(p: (proc (cbproc: proc ()) {...}{.gcsafe.})) {...}{.raises: [], tags: [].} - 
    Change current implementation of 
callSoon. This is normally called when dispatcher fromasyncdispatcheris initialized. Source Edit - 
    
proc callSoon(cbproc: proc ()) {...}{.raises: [Exception], tags: [RootEffect].} - 
    
Call
cbproc"soon".If async dispatcher is running,
cbprocwill be executed during next dispatcher tick.If async dispatcher is not running,
Source Editcbprocwill be executed immediately. - 
    
proc newFuture[T](fromProc: string = "unspecified"): owned(Future[T]) - 
    
Creates a new future.
Specifying
Source EditfromProc, which is a string specifying the name of the proc that this future belongs to, is a good habit as it helps with debugging. - 
    
proc newFutureVar[T](fromProc = "unspecified"): owned(FutureVar[T]) - 
    
Create a new
FutureVar. This Future type is ideally suited for situations where you want to avoid unnecessary allocations of Futures.Specifying
Source EditfromProc, which is a string specifying the name of the proc that this future belongs to, is a good habit as it helps with debugging. - 
    
proc clean[T](future: FutureVar[T]) - 
    Resets the 
finishedstatus offuture. Source Edit - 
    
proc complete[T](future: Future[T]; val: T) - 
    Completes 
futurewith valueval. Source Edit - 
    
proc complete(future: Future[void]) {...}{.raises: [Exception], tags: [RootEffect].} - 
    Completes a void 
future. Source Edit - 
    
proc complete[T](future: FutureVar[T]) - 
    Completes a 
FutureVar. Source Edit - 
    
proc complete[T](future: FutureVar[T]; val: T) - 
    
Completes a
FutureVarwith valueval.Any previously stored value will be overwritten.
Source Edit - 
    
proc fail[T](future: Future[T]; error: ref Exception) - 
    Completes 
futurewitherror. Source Edit - 
    
proc clearCallbacks(future: FutureBase) {...}{.raises: [], tags: [].} - Source Edit
 - 
    
proc addCallback(future: FutureBase; cb: proc () {...}{.closure, gcsafe.}) {...}{. raises: [Exception], tags: [RootEffect].} - 
    
Adds the callbacks proc to be called when the future completes.
If future has already completed then
Source Editcbwill be called immediately. - 
    
proc addCallback[T](future: Future[T]; cb: proc (future: Future[T]) {...}{.closure, gcsafe.}) - 
    
Adds the callbacks proc to be called when the future completes.
If future has already completed then
Source Editcbwill be called immediately. - 
    
proc callback=(future: FutureBase; cb: proc () {...}{.closure, gcsafe.}) {...}{. raises: [Exception], tags: [RootEffect].} - 
    
Clears the list of callbacks and sets the callback proc to be called when the future completes.
If future has already completed then
cbwill be called immediately.It's recommended to use
Source EditaddCallbackortheninstead. - 
    
proc callback=[T](future: Future[T]; cb: proc (future: Future[T]) {...}{.closure, gcsafe.}) - 
    
Sets the callback proc to be called when the future completes.
If future has already completed then
Source Editcbwill be called immediately. - 
    
proc `$`(stackTraceEntries: seq[StackTraceEntry]): string {...}{. raises: [ValueError], tags: [].} - Source Edit
 - 
    
proc read[T](future: Future[T] | FutureVar[T]): T - 
    
Retrieves the value of
future. Future must be finished otherwise this function will fail with aValueErrorexception.If the result of the future is an error then that error will be raised.
Source Edit - 
    
proc readError[T](future: Future[T]): ref Exception - 
    
Retrieves the exception stored in
future.An
Source EditValueErrorexception will be thrown if no exception exists in the specified Future. - 
    
proc mget[T](future: FutureVar[T]): var T - 
    
Returns a mutable value stored in
future.Unlike
Source Editread, this function will not raise an exception if the Future has not been finished. - 
    
proc finished(future: FutureBase | FutureVar): bool - 
    
Determines whether
futurehas completed.
Source EditTruemay indicate an error or a value. Usefailedto distinguish. - 
    
proc failed(future: FutureBase): bool {...}{.raises: [], tags: [].} - 
    Determines whether 
futurecompleted with an error. Source Edit - 
    
proc asyncCheck[T](future: Future[T]) - 
    
Sets a callback on
futurewhich raises an exception if the future finished with an error.This should be used instead of
Source Editdiscardto discard void futures, or usewaitForif you need to wait for the future's completion. - 
    
proc `and`[T, Y](fut1: Future[T]; fut2: Future[Y]): Future[void] - 
    Returns a future which will complete once both 
fut1andfut2complete. Source Edit - 
    
proc `or`[T, Y](fut1: Future[T]; fut2: Future[Y]): Future[void] - 
    Returns a future which will complete once either 
fut1orfut2complete. Source Edit - 
    
proc all[T](futs: varargs[Future[T]]): auto - 
    
Returns a future which will complete once all futures in
futscomplete. If the argument is empty, the returned future completes immediately.If the awaited futures are not
Future[void], the returned future will hold the values of all awaited futures in a sequence.If the awaited futures are
Source EditFuture[void], this proc returnsFuture[void]. 
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
 https://nim-lang.org/docs/asyncfutures.html