Class Async
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 TypeMethodDescriptionstatic <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>
Tawait(org.teavm.jso.core.JSPromise<T> promise) Waits for a promise and returns its value.static voidawaitVoid(org.teavm.jso.core.JSPromise<?> promise) Waits for a promise whose value is not wanted.static <T extends org.teavm.jso.JSObject>
Trace(org.teavm.jso.core.JSPromise<T>... promises) Waits for whichever promise settles first.static Future<org.teavm.jso.JSObject> Runs work on its own fiber, with no result.static voidsleep(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> Runs work on its own fiber, so the caller does not wait for it.static org.teavm.jso.JSObjectTurns a Java throwable into something JavaScript can reject with.
-
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
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.waitUntilto hold the invocation open for it.- Type Parameters:
T- what the work produces- Parameters:
work- the work- Returns:
- a future for the result
-
run
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
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
undefinedfor one rather than null. Rejecting a promise withundefinedloses the failure: a waiter resuming from it has nothing to rethrow, so it does not resume at all. AnErrorcarrying the Java message is what makes the rejection observable.- Parameters:
failure- the failure- Returns:
- the JavaScript value
-