Emulating CompletableFuture with Promise

510 views
Skip to first unread message

Ian Preston

unread,
Sep 4, 2016, 8:28:27 PM9/4/16
to GWT Users
I'm trying to emulate CompletableFuture by backing directly onto Promise, but when compiling it gives this error: 
"No source code is available for type java.util.concurrent.CompletableFuture<T>"
(it finds other JRE classes I've replaced fine)

My source I'm including is below. Am I doing anything illegal?
********************************************
package java.util.concurrent;

import com.google.gwt.user.client.*;
import jsinterop.annotations.*;

import java.util.function.*;

@JsType(isNative=true, name = "Promise", namespace = JsPackage.GLOBAL)
public class CompletableFuture<T> implements CompletionStage<T> {

    @JsMethod(name = "then")
    public native <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn);

    @JsMethod(name = "then")
    public native CompletableFuture<Void> thenAccept(Consumer<? super T> action);

    @JsMethod(name = "then")
    public native <U> CompletableFuture<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);

    @JsMethod(name = "reject")
    public native boolean completeExceptionally(Throwable ex);

    @JsMethod(name = "resolve")
    public native boolean complete(T value);

    @JsMethod(name = "resolve")
    public static native <U> CompletableFuture<U> completedFuture(U value);

    @JsOverlay
    public T get() throws InterruptedException, ExecutionException {
        Window.alert("Calling synchronous get() on CompletableFuture is not possible in Javascript!");
        throw new IllegalStateException("Unimplemented!");
    }
}

Andrei Korzhevskii

unread,
Sep 5, 2016, 4:47:10 AM9/5/16
to GWT Users
I guess you need to super-source this class so the gwt compiler could use it.

You can take a look at my initial CompletableFuture implementation here:


Feel free to use it but note that I haven't tested it properly yet.

Ian Preston

unread,
Sep 5, 2016, 6:48:09 PM9/5/16
to GWT Users
Thanks, Andrei. That looks great! Is there a reason you've changed the signatures on some of the CompletableFuture methods?

E.g. in the JRE,
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)

has become

public <U> CompletionStage<U> thenApply(Function<? super T, ? extends U> fn)

Andrei Korzhevskii

unread,
Sep 6, 2016, 7:16:36 AM9/6/16
to GWT Users
No reason for that. I had to implement the code from scratch and didnt pay attention to the signatures. 
Thanks for the hint!

Ian Preston

unread,
Sep 6, 2016, 3:09:28 PM9/6/16
to GWT Users
For another hint, I think you don't want the "new" before "Promise.resolve" on line 88 of impl/JsPromise :-)
What the best way for me to report bugs to you? A PR?

Andrei Korzhevskii

unread,
Sep 7, 2016, 4:24:37 AM9/7/16
to GWT Users
Sounds good. Thanks!

Andrei Korzhevskii

unread,
Sep 9, 2016, 9:40:40 AM9/9/16
to GWT Users
In this case I wanted 'new Promise', not 'Promise.resolve'. Thanks for spotting that out! I've updated my branch.


On Tuesday, September 6, 2016 at 10:09:28 PM UTC+3, Ian Preston wrote:

James Horsley

unread,
Sep 9, 2016, 10:06:47 AM9/9/16
to GWT Users
This is brilliant. I would love to see this in GWT proper. Maybe even a separate library temporarily since it won't make the 2.8.0 release train?

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-tool...@googlegroups.com.
To post to this group, send email to google-we...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Benjamin DeLillo

unread,
Sep 6, 2017, 4:55:14 PM9/6/17
to GWT Users
I decided to package this up in a standalone library so it can be used until such time that it makes it into a GWT release.

Peter Donald

unread,
Sep 8, 2017, 7:18:38 AM9/8/17
to GWT Mailing List
That is pretty awesome. Thanks!

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsub...@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.



--
Cheers,

Peter Donald
Reply all
Reply to author
Forward
0 new messages