Class Async

java.lang.Object
dev.gmitch215.bytebox.concurrent.Async

public final class Async extends Object
Waiting on JavaScript from Java.

Everything on this platform that touches the network, a binding, or a stream is a JavaScript promise. await(JSPromise) turns one into an ordinary blocking call: the compiler rewrites the caller into a continuation, and the host resumes it when the promise settles. That is why every binding on Env reads as a plain method rather than returning a future.

Suspension is real but parallelism is not. Cloudflare Workers run one thread and do not provide the Web Worker API, so a Java thread here is a fiber on the host's queue and two of them never run at once.

Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends org.teavm.jso.JSObject>
    List<T>
    all(org.teavm.jso.core.JSPromise<T>... promises)
    Waits for every promise, in order.
    static <T extends org.teavm.jso.JSObject>
    T
    await(org.teavm.jso.core.JSPromise<T> promise)
    Waits for a promise and returns its value.
    static void
    awaitVoid(org.teavm.jso.core.JSPromise<?> promise)
    Waits for a promise whose value is not wanted.
    static <T extends org.teavm.jso.JSObject>
    T
    race(org.teavm.jso.core.JSPromise<T>... promises)
    Waits for whichever promise settles first.
    static Future<org.teavm.jso.JSObject>
    run(Runnable work)
    Runs work on its own fiber, with no result.
    static void
    sleep(int millis)
    Sleeps without occupying the fiber, using the host's timer rather than a spin.
    static <T extends org.teavm.jso.JSObject>
    Future<T>
    supply(Supplier<T> work)
    Runs work on its own fiber, so the caller does not wait for it.
    static org.teavm.jso.JSObject
    toJs(Throwable failure)
    Turns a Java throwable into something JavaScript can reject with.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • await

      public static <T extends org.teavm.jso.JSObject> T await(org.teavm.jso.core.JSPromise<T> promise)
      Waits for a promise and returns its value.

      Suspends the calling fiber. Not callable from a module initializer, where nothing is draining the queue, nor from a function handed to JavaScript, where a JavaScript frame on the stack makes suspension impossible.

      Type Parameters:
      T - the resolved type
      Parameters:
      promise - the promise
      Returns:
      the resolved value
    • awaitVoid

      public static void awaitVoid(org.teavm.jso.core.JSPromise<?> promise)
      Waits for a promise whose value is not wanted.
      Parameters:
      promise - the promise
    • all

      @SafeVarargs public static <T extends org.teavm.jso.JSObject> List<T> all(org.teavm.jso.core.JSPromise<T>... promises)
      Waits for every promise, in order.

      The promises were already in flight, so this costs one suspension rather than one per promise.

      Type Parameters:
      T - the resolved type
      Parameters:
      promises - the promises
      Returns:
      the resolved values, in the order given
    • race

      @SafeVarargs public static <T extends org.teavm.jso.JSObject> T race(org.teavm.jso.core.JSPromise<T>... promises)
      Waits for whichever promise settles first.
      Type Parameters:
      T - the resolved type
      Parameters:
      promises - the promises
      Returns:
      the first value to arrive
    • supply

      public static <T extends org.teavm.jso.JSObject> Future<T> supply(Supplier<T> work)
      Runs work on its own fiber, so the caller does not wait for it.

      Whether the work finishes depends on the host draining its queue. Register the returned future with ctx.waitUntil to hold the invocation open for it.

      Type Parameters:
      T - what the work produces
      Parameters:
      work - the work
      Returns:
      a future for the result
    • run

      public static Future<org.teavm.jso.JSObject> run(Runnable work)
      Runs work on its own fiber, with no result.
      Parameters:
      work - the work
      Returns:
      a future that completes when the work does
    • sleep

      public static void sleep(int millis)
      Sleeps without occupying the fiber, using the host's timer rather than a spin.

      Workers pin the clock between I/O, so a timer is also the only thing that makes it advance.

      Parameters:
      millis - how long to wait
    • toJs

      public static org.teavm.jso.JSObject toJs(Throwable failure)
      Turns a Java throwable into something JavaScript can reject with.

      A throwable that never came from JavaScript has no wrapper to hand back, and the runtime answers undefined for one rather than null. Rejecting a promise with undefined loses the failure: a waiter resuming from it has nothing to rethrow, so it does not resume at all. An Error carrying the Java message is what makes the rejection observable.

      Parameters:
      failure - the failure
      Returns:
      the JavaScript value