Quirrel4.41.0

async.Future.resolve

Method of Future, from module "async"
instance.resolve([value])

Settles a pending Future with value (default null); ignored if already settled

examples/async/future/resolve-basic.nut
from "async" import Future

let quest = Future()
quest.resolve("dragon slain")

println("quest.getValue() =", quest.getValue())
Output:
quest.getValue() = dragon slain

Parameters

valueanythe value to fulfil with; null when omitted optional

Errors

Throws cannot resolve a future with itself for p.resolve(p): storing a future as its own value would root it through the runtime's internal value table, with nothing left to release it. This is the only cycle this method guards against: an indirect cycle such as a.resolve(b); b.resolve(a) is not caught, and leaks the pair until the VM closes.

Throws cannot resolve the future returned by an async function for a task-future, the future an async function call returns. Its only way to settle is that function's own return or throw; every other caller, including the function's own caller, can only read it.

Notes

Ignored with no error on a future that has already settled, whether fulfilled or faulted - see getState.

value is stored verbatim: resolving with another Future does not adopt or flatten it. The result is a future that holds a future, and a consumer peels one level per await, as described for async return values.

Example

examples/async/future/resolve.nut
from "async" import Future

let p = Future()
p.resolve(1)
p.resolve(2)               // no-op: already settled
println("p.getValue() =", p.getValue())      // 1

let q = Future()
try { q.resolve(q) } catch (e) { println("q.resolve(q) throws:", e) }   // self-resolve: rejected

async function task() { return 42 }
let t = task()
try { t.resolve(0) } catch (e) { println("t.resolve(0) throws:", e) }   // task-futures settle only via return/throw
Output:
p.getValue() = 1
q.resolve(q) throws: cannot resolve a future with itself
t.resolve(0) throws: cannot resolve the future returned by an async function

See also

rejectFaults a pending Future with value (default null); ignored if already settled
getValueReturns the settled value (fulfilled or fault value); throws while pending. A pure peek: reading a fault does not acknowledge it (see markHandled)
getStateReturns the lifecycle state: 'pending', 'fulfilled' or 'faulted'
Futureclass index