Package dev.gmitch215.bytebox.concurrent
Class Future<T extends org.teavm.jso.JSObject>
java.lang.Object
dev.gmitch215.bytebox.concurrent.Future<T>
- Type Parameters:
T- the value type
A value that is not here yet, backed by a JavaScript promise.
java.util.concurrent.Future and CompletableFuture exist on this platform
(bytebox supplies them, because the class library does not), but they have no pool behind them and
know nothing about promises. This is the type to use when the work is a JavaScript promise: it
carries the part of that API which means something here, over the promise the runtime already
understands.
Most code does not need it. A binding call blocks and returns a value, which is more direct than a future and costs the same. Reach for this to hold work open past a handler's return, to start several operations before waiting on any of them, or to hand a promise back to JavaScript.
Future<Response> primary = Async.supply(() -> fetchPrimary(env));
Future<Response> backup = Async.supply(() -> fetchBackup(env));
ctx.waitUntil(audit(env).promise());
return primary.orElseGet(backup::join);
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends org.teavm.jso.JSObject>
Future<T> completed(T value) A future that is already done.static <T extends org.teavm.jso.JSObject>
Future<T> A future that has already failed.join()Waits for the value.<R extends org.teavm.jso.JSObject>
Future<R> Transforms the value once it arrives, on a fiber of its own.static <T extends org.teavm.jso.JSObject>
Future<T> of(org.teavm.jso.core.JSPromise<T> promise) Adopts an existing promise.Waits for the value, answering a fallback if the future failed.Waits for the value, calling a supplier if the future failed.org.teavm.jso.core.JSPromise<T> promise()Returns the underlying promise, to hand to JavaScript or toctx.waitUntil.Handles a failure without unwinding.<R extends org.teavm.jso.JSObject>
Future<R> Chains another asynchronous step after this one.
-
Method Details
-
of
public static <T extends org.teavm.jso.JSObject> Future<T> of(org.teavm.jso.core.JSPromise<T> promise) Adopts an existing promise.- Type Parameters:
T- the value type- Parameters:
promise- the promise- Returns:
- a future over it
-
completed
A future that is already done.- Type Parameters:
T- the value type- Parameters:
value- the value- Returns:
- the completed future
-
failed
A future that has already failed.- Type Parameters:
T- the value type- Parameters:
failure- the failure- Returns:
- the failed future
-
promise
Returns the underlying promise, to hand to JavaScript or toctx.waitUntil.- Returns:
- the underlying promise, to hand to JavaScript or to
ctx.waitUntil
-
join
Waits for the value.Suspends the calling fiber, and rethrows whatever the future failed with.
- Returns:
- the value
-
orElse
Waits for the value, answering a fallback if the future failed.- Parameters:
fallback- the value to use on failure- Returns:
- the value, or the fallback
-
orElseGet
Waits for the value, calling a supplier if the future failed.- Parameters:
fallback- produces the value to use on failure- Returns:
- the value, or what the fallback produced
-
map
Transforms the value once it arrives, on a fiber of its own.- Type Parameters:
R- the new value type- Parameters:
mapper- the transformation- Returns:
- a future for the transformed value
-
then
Chains another asynchronous step after this one.- Type Parameters:
R- the new value type- Parameters:
next- produces the next future- Returns:
- a future for the chained result
-
recover
Handles a failure without unwinding.- Parameters:
recovery- produces a replacement value from the failure- Returns:
- a future that does not fail
-