Future
Object Hierarchy:
Description:
public class Future<T> : Object
Represents the eventual result of an asynchronous computation.
Future supports success/failure state tracking, blocking wait, timeout wait, transformation, and failure recovery.
Example:
var future = Future<int>.run (() => {
return 40 + 2;
});
int answer = future.orElse (0);
assert (answer == 42);
Content:
Static methods:
- public static Future<ArrayList<T>> all<T> (ArrayList<Future<T>> futures)
Waits all futures and returns list of success values.
- public static Future<ArrayList<Future<T>>> allSettled<T> (ArrayList<Future<T>> futures)
Waits until all source futures complete and returns them.
- public static Future<T> any<T> (ArrayList<Future<T>> futures)
Returns first completed future result.
- public static Future<T> completed<T> (owned T value)
Creates an already successful future.
- public static Future<T> delayed<T> (Duration delay, owned TaskFunc<T> task)
Starts task after delay.
- public static Future<T> failed<T> (string message)
Creates an already failed future.
- public static Future<T> race<T> (ArrayList<Future<T>> futures)
Alias of
any. Returns first completed result.
- public static Future<T> run<T> (owned TaskFunc<T> task)
Starts asynchronous execution on a background thread.
Methods:
- public T await ()
Waits until completion and returns success value.
- public T awaitTimeout (Duration timeout)
Waits for completion up to timeout.
- public bool cancel ()
Requests cancellation.
- public string? error ()
Returns failure reason.
- public Future<U> flatMap<U> (owned MapFunc<T,Future<U>> fn)
Chains another asynchronous computation.
- public bool isCancelled ()
Returns whether this future is cancelled.
- public bool isDone ()
Returns true when this future has completed.
- public bool isFailed ()
Returns true when this future failed.
- public bool isSuccess ()
Returns true when this future completed successfully.
- public Future<U> map<U> (owned MapFunc<T,U> fn)
Transforms the successful value.
- public Future<T> onComplete (owned ConsumerFunc<T> fn)
Registers callback invoked when this future completes.
- public T orElse (T fallback)
Returns success value or fallback when failed/cancelled.
- public Future<T> recover (owned RecoverFunc<T> fn)
Recovers from failure by producing a fallback value.
- public Future<T> timeout (Duration timeout)
Applies timeout to this future and returns timed future.
Inherited Members:
All known members inherited from class GLib.Object
- @get
- @new
- @ref
- @set
- add_toggle_ref
- add_weak_pointer
- bind_property
- connect
- constructed
- disconnect
- dispose
- dup_data
- dup_qdata
- force_floating
- freeze_notify
- get_class
- get_data
- get_property
- get_qdata
- get_type
- getv
- interface_find_property
- interface_install_property
- interface_list_properties
- is_floating
- new_valist
- new_with_properties
- newv
- notify
- notify_property
- ref_count
- ref_sink
- remove_toggle_ref
- remove_weak_pointer
- replace_data
- replace_qdata
- set_data
- set_data_full
- set_property
- set_qdata
- set_qdata_full
- set_valist
- setv
- steal_data
- steal_qdata
- thaw_notify
- unref
- watch_closure
- weak_ref
- weak_unref