hxn

Future

  • Type parameters: T

The single result or failure of an asynchronous operation.

await() returns the value or throws the failure. It blocks the calling thread but lets the garbage collector run. then() delivers the value or failure later, the next time the library's results are delivered: see Loop.

A binding constructs one from its library's Inbox and four functions: watch registers the future with the library's queue and returns its id, state returns the state (0 pending, 1 resolved, 2 rejected), wait takes a timeout in milliseconds (negative for no timeout) and returns the state, and take returns the value or throws. The result stays in the library until take runs on a Haxe thread, so the native thread that settles the future never calls into this runtime.

map, flatMap and recover derive a future from another, and resolved and rejected create settled ones. A derived future settles during its source's delivery, and await on it waits for the source.

Constructor

function new(?inbox:Inbox, ?watch:() -> Int, ?state:() -> Int, ?wait:(Int) -> Int, ?take:() -> T)

Static methods

function resolved(value:T):Future<T>

A future that already has value.

function rejected(failure:Exception):Future<T>

A future that already failed with failure.

Methods

function isReady():Bool

True once the future has a value or a failure.

function isRejected():Bool
function await():T

Waits for the result. Any thread may call it, any number of times.

function then(onValue:(T) -> Void, ?onError:(Exception) -> Void):Void

Calls onValue or onError after the future has settled, the next time the library's results are delivered. That happens on the event loop of the thread that first requested a delivery from this library, or in Loop.sync when the app drives deliveries itself. On a future made in Haxe that has settled, it calls at once.

If the future failed and there is no onError, the failure is thrown from that delivery.

function map(transform:(T) -> R):Future<R>

A future of transform applied to this future's value. A failure passes through unchanged.

function flatMap(transform:(T) -> Future<R>):Future<R>

A future that settles like the future transform returns for this future's value. A failure passes through unchanged.

function recover(transform:(Exception) -> Future<T>):Future<T>

A future with this future's value, or, if this one fails, the result of the future transform returns for the failure.