dreamworkAPI
Ctrl K
GitHub
classDocs / Future

Future

classsharedmenusource
Object → Future

Futures are objects that hold the result that can be assigned asynchronously they can be awaited to get the result or add callback with :addCallback(...) method.

lua
local fut = futures.Future()

fut:addCallback( function( fut )
    print( fut:result() ) -- "hello world"
end )

fut:setResult( "hello world" )

-- or you can await it

---@async
local function main()
    print( fut:await() ) -- "hello world"
end

futures.run( main )

-- also you can set error or cancel it
fut:setError( "something went wrong" )
fut:cancel()

Constructor

Future(): Future

Methods

addCallbackAdds callback that will be called when future is done if future is already done, callback will be called immediately.
awaitAwait until future will be finished if it contains an error, then it will be thrown.
cancelTries to cancel future, if it's already done, returns false otherwise marks it as cancelled, runs all callbacks and ret…
errorReturns the future's error, if any: * if cancelled, returns RuntimeError "future was cancelled" * if still pending, ret…
isCancelledReturns true if Future was cancelled.
isFinishedReturns true if Future is finished (or cancelled).
isPendingReturns true if Future is pending.
removeCallbackRemoves callback that was previously added with :addCallback.
resultReturns result if future is finished otherwise throws an error.
setErrorSets error of the Future, marks it as finished, and runs all callbacks if future is already finished, error will be thr…
setResultSets result of the Future, marks it as finished, and runs all callbacks if future is already finished, error will be th…

#Future:addCallback

sharedmenusource
function Future:addCallback(fn: fun( fut: Future<T> ))

Adds callback that will be called when future is done if future is already done, callback will be called immediately.

Parameters
NameTypeDescription
fnfun( fut: Future<T> )The callback to invoke with the future once it's done.
See also
  • Future.removeCallback for removing callback

#Future:await

sharedmenuasyncsource
async function Future:await(): T

Await until future will be finished if it contains an error, then it will be thrown.

Returns
TypeNameDescription
T

#Future:cancel

sharedmenusource
function Future:cancel(): boolean

Tries to cancel future, if it's already done, returns false otherwise marks it as cancelled, runs all callbacks and returns true.

Returns
TypeNameDescription
booleancancelled

#Future:error

sharedmenunodiscardsource
function Future:error(): Error | nil

Returns the future's error, if any:

  • if cancelled, returns RuntimeError "future was cancelled"
  • if still pending, returns RuntimeError "future is not finished"
  • if finished successfully, returns nil
  • if finished with an error, returns that error
Returns
TypeNameDescription
Error | nil
See also
  • Future.setError

#Future:isCancelled

sharedmenunodiscardsource
function Future:isCancelled(): boolean

Returns true if Future was cancelled.

Returns
TypeNameDescription
boolean

#Future:isFinished

sharedmenunodiscardsource
function Future:isFinished(): boolean

Returns true if Future is finished (or cancelled).

Returns
TypeNameDescription
boolean

#Future:isPending

sharedmenunodiscardsource
function Future:isPending(): boolean

Returns true if Future is pending.

Returns
TypeNameDescription
boolean

#Future:removeCallback

sharedmenusource
function Future:removeCallback(fn: function)

Removes callback that was previously added with :addCallback.

Parameters
NameTypeDescription
fnfunctionThe callback previously passed to :addCallback.
See also
  • Future.addCallback for adding callback

#Future:result

sharedmenunodiscardsource
function Future:result(): T

Returns result if future is finished otherwise throws an error.

Returns
TypeNameDescription
T
See also
  • Future.setResult

#Future:setError

sharedmenusource
function Future:setError(err: string | Error, error_class?: ErrorClass)

Sets error of the Future, marks it as finished, and runs all callbacks if future is already finished, error will be thrown.

Strings and other values are wrapped into error_class, an Error without a call stack captures the stack of the caller, an Error with one is kept as is.

Parameters
NameTypeDescription
errstring | ErrorThe error to fail the future with.
error_classoptionalErrorClassThe class used to wrap err if it is not an Error, defaults to RuntimeError.
See also
  • Future.error to retrieve error

#Future:setResult

sharedmenusource
function Future:setResult(result: T)

Sets result of the Future, marks it as finished, and runs all callbacks if future is already finished, error will be thrown.

Parameters
NameTypeDescription
resultTThe value to resolve the future with.
See also
  • Future.result to retrieve result
  • Future.await to asynchronously retrieve result

Subclasses

futures.Task

Generated from the dreamwork meta branch.