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

java.lang.Object
dev.gmitch215.bytebox.concurrent.Deferred<T>
Type Parameters:
T - what the promise resolves to

public final class Deferred<T extends org.teavm.jso.JSObject> extends Object
A JavaScript promise that Java settles.

This is what lets a handler suspend. A promise's executor runs synchronously and with a JavaScript frame on the stack, so a suspending call inside one fails; capturing the two settle functions instead moves the work outside that frame, onto a fiber that is free to suspend.

Every trigger's exported entry point is built this way, which is why the host sees a promise returned immediately and resolved only once the handler has genuinely finished.

 Deferred<Response> deferred = Deferred.create();
 new Thread(() -> {
 	try {
 		deferred.resolve(handler.fetch(request, env, ctx));
 	} catch (Throwable failure) {
 		deferred.reject(failure);
 	}
 }).start();
 return deferred.promise();
Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T extends org.teavm.jso.JSObject>
    Deferred<T>
     
    org.teavm.jso.core.JSPromise<T>
    Returns the promise, to hand back to JavaScript.
    void
    reject(Throwable failure)
    Rejects the promise with a Java throwable, which crosses back as a JavaScript error.
    void
    reject(org.teavm.jso.core.JSError error)
    Rejects the promise with a JavaScript value.
    void
    resolve(T value)
    Resolves the promise.

    Methods inherited from class java.lang.Object

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

    • create

      public static <T extends org.teavm.jso.JSObject> Deferred<T> create()
      Type Parameters:
      T - what the promise resolves to
      Returns:
      a promise nothing has settled yet
    • promise

      public org.teavm.jso.core.JSPromise<T> promise()
      Returns the promise, to hand back to JavaScript.
      Returns:
      the promise, to hand back to JavaScript
    • resolve

      public void resolve(T value)
      Resolves the promise.
      Parameters:
      value - the value to resolve with, which may be null
    • reject

      public void reject(Throwable failure)
      Rejects the promise with a Java throwable, which crosses back as a JavaScript error.
      Parameters:
      failure - the failure
    • reject

      public void reject(org.teavm.jso.core.JSError error)
      Rejects the promise with a JavaScript value.
      Parameters:
      error - the error