async.Future.resolve
Settles a pending Future with value (default null); ignored if already settled
from "async" import Future
let quest = Future()
quest.resolve("dragon slain")
println("quest.getValue() =", quest.getValue())quest.getValue() = dragon slainParameters
value | any | the 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
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/throwp.getValue() = 1
q.resolve(q) throws: cannot resolve a future with itself
t.resolve(0) throws: cannot resolve the future returned by an async functionSee also
reject | Faults a pending Future with value (default null); ignored if already settled |
getValue | Returns the settled value (fulfilled or fault value); throws while pending. A pure peek: reading a fault does not acknowledge it (see markHandled) |
getState | Returns the lifecycle state: 'pending', 'fulfilled' or 'faulted' |
| Future | class index |