Class Future<T extends org.teavm.jso.JSObject>

java.lang.Object
dev.gmitch215.bytebox.concurrent.Future<T>
Type Parameters:
T - the value type

public final class Future<T extends org.teavm.jso.JSObject> extends Object
A value that is not here yet, backed by a JavaScript promise.

java.util.concurrent.CompletableFuture does not exist on this platform: the class library ships a small subset of java.util.concurrent with no Future at all, and a thread pool cannot be built on a runtime with one thread. This 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 Type
    Method
    Description
    static <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>
    failed(Throwable failure)
    A future that has already failed.
    Waits for the value.
    <R extends org.teavm.jso.JSObject>
    Future<R>
    map(Function<T,R> mapper)
    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.
    orElse(T fallback)
    Waits for the value, answering a fallback if the future failed.
    orElseGet(Supplier<T> fallback)
    Waits for the value, calling a supplier if the future failed.
    org.teavm.jso.core.JSPromise<T>
    Returns the underlying promise, to hand to JavaScript or to ctx.waitUntil.
    Handles a failure without unwinding.
    <R extends org.teavm.jso.JSObject>
    Future<R>
    then(Function<T,Future<R>> next)
    Chains another asynchronous step after this one.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static <T extends org.teavm.jso.JSObject> Future<T> completed(T value)
      A future that is already done.
      Type Parameters:
      T - the value type
      Parameters:
      value - the value
      Returns:
      the completed future
    • failed

      public static <T extends org.teavm.jso.JSObject> Future<T> failed(Throwable failure)
      A future that has already failed.
      Type Parameters:
      T - the value type
      Parameters:
      failure - the failure
      Returns:
      the failed future
    • promise

      public org.teavm.jso.core.JSPromise<T> promise()
      Returns the underlying promise, to hand to JavaScript or to ctx.waitUntil.
      Returns:
      the underlying promise, to hand to JavaScript or to ctx.waitUntil
    • join

      public T join()
      Waits for the value.

      Suspends the calling fiber, and rethrows whatever the future failed with.

      Returns:
      the value
    • orElse

      public T orElse(T fallback)
      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

      public T orElseGet(Supplier<T> fallback)
      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

      public <R extends org.teavm.jso.JSObject> Future<R> map(Function<T,R> mapper)
      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

      public <R extends org.teavm.jso.JSObject> Future<R> then(Function<T,Future<R>> next)
      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

      public Future<T> recover(Function<Throwable,T> recovery)
      Handles a failure without unwinding.
      Parameters:
      recovery - produces a replacement value from the failure
      Returns:
      a future that does not fail