Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose wrapPromise and Thenable in the GraalJS public API #811

Open
mikehearn opened this issue Mar 21, 2024 · 0 comments
Open

Expose wrapPromise and Thenable in the GraalJS public API #811

mikehearn opened this issue Mar 21, 2024 · 0 comments

Comments

@mikehearn
Copy link
Contributor

In the unit tests for promises we find:

private static Value wrapPromise(Context context, CompletableFuture<String> javaFuture) {
        Value global = context.getBindings("js");
        Value promiseConstructor = global.getMember("Promise");
        return promiseConstructor.newInstance((ProxyExecutable) arguments -> {
            Value resolve = arguments[0];
            Value reject = arguments[1];
            javaFuture.whenComplete((result, ex) -> {
                if (result != null) {
                    resolve.execute(result);
                } else {
                    reject.execute(ex);
                }
            });
            // return value of function(resolve,reject) is ignored by `new Promise()`.
            return null;
        });
    }

    public interface Thenable {
        void then(Value onResolve, Value onReject);
    }

These look useful! It'd be great if these conversions were done automatically, rather than being a bit of utility code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant