Package dev.gmitch215.bytebox.concurrent
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
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 TypeMethodDescriptionstatic <T extends org.teavm.jso.JSObject>
Deferred<T> create()org.teavm.jso.core.JSPromise<T> promise()Returns the promise, to hand back to JavaScript.voidRejects the promise with a Java throwable, which crosses back as a JavaScript error.voidreject(org.teavm.jso.core.JSError error) Rejects the promise with a JavaScript value.voidResolves the promise.
-
Method Details
-
create
- Type Parameters:
T- what the promise resolves to- Returns:
- a promise nothing has settled yet
-
promise
Returns the promise, to hand back to JavaScript.- Returns:
- the promise, to hand back to JavaScript
-
resolve
Resolves the promise.- Parameters:
value- the value to resolve with, which may benull
-
reject
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
-