Future is an implementation of Futures and Promises, with the exception that in addition to "success" and "failure" states (represented as "complete" and "error"), Lime Future introduces "progress" feedback as well to increase the value of Future values.

var future = Image.loadFromFile ("image.png");
future.onComplete (function (image) { trace ("Image loaded"); });
future.onProgress (function (loaded, total) { trace ("Loading: " + loaded + ", " + total); });
future.onError (function (error) { trace (error); });

Image.loadFromFile ("image.png").then (function (image) {

	return Future.withValue (image.width);

}).onComplete (function (width) { trace (width); })

Future values can be chained together for asynchronous processing of values.

If an error occurs earlier in the chain, the error is propagated to all onError callbacks.

Future will call onComplete callbacks, even if completion occurred before registering the callback. This resolves race conditions, so even functions that return immediately can return values using Future.

Future values are meant to be immutable, if you wish to update a Future, you should create one using a Promise, and use the Promise interface to influence the error, complete or progress state of a Future.

Static methods

@:value({ onProgress : null, onError : null })staticofEvents<T>(onComplete:_Event_ofEvents_T_Void<T ‑> Void>, ?onError:Null<_Event_Dynamic_Void<Dynamic ‑> Void>>, ?onProgress:Null<_Event_Int_Int_Void<(Int, Int) ‑> Void>>):Future<T>

Create a new Future instance based on complete and (optionally) error and/or progress Event instances

staticwithError(error:Dynamic):Future<Dynamic>

Creates a Future instance which has finished with an error value

Parameters:

error

The error value to set

Returns:

A new Future instance

staticwithValue<T>(value:T):Future<T>

Creates a Future instance which has finished with a completion value

Parameters:

value

The completion value to set

Returns:

A new Future instance

Constructor

@:value({ useThreads : false, work : null })new(?work:() ‑> T, useThreads:Bool = false)

Parameters:

work

Optional: a function to compute this future's value.

useThreads

Whether to run work on a background thread, where supported. If false or if this isn't a system target, it will run immediately on the main thread.

Variables

read onlyerror:Dynamic

If the Future has finished with an error state, the error value

read onlyisComplete:Bool

Whether the Future finished with a completion state

read onlyisError:Bool

Whether the Future finished with an error state

read onlyvalue:T

If the Future has finished with a completion state, the completion value

Methods

onComplete(listener:T ‑> Void):Future<T>

Register a listener for when the Future completes.

If the Future has already completed, this is called immediately with the result

Parameters:

listener

A callback method to receive the result value

Returns:

The current Future

onError(listener:Dynamic ‑> Void):Future<T>

Register a listener for when the Future ends with an error state.

If the Future has already ended with an error, this is called immediately with the error value

Parameters:

listener

A callback method to receive the error value

Returns:

The current Future

onProgress(listener:(Int, Int) ‑> Void):Future<T>

Register a listener for when the Future updates progress.

If the Future is already completed, this will not be called.

Parameters:

listener

A callback method to receive the progress value

Returns:

The current Future

@:value({ waitTime : -1 })ready(waitTime:Int = -1):Future<T>

Attempts to block on an asynchronous Future, returning when it is completed.

Parameters:

waitTime

(Optional) A timeout before this call will stop blocking

Returns:

This current Future

@:value({ waitTime : -1 })result(waitTime:Int = -1):Null<T>

Attempts to block on an asynchronous Future, returning the completion value when it is finished.

Parameters:

waitTime

(Optional) A timeout before this call will stop blocking

Returns:

The completion value, or null if the request timed out or blocking is not possible

then<U>(next:T ‑> Future<U>):Future<U>

Chains two Future instances together, passing the result from the first as input for creating/returning a new Future instance of a new or the same type