jQuery.Deferred() / Promises functionality in GWT?
Asked Answered
T

2

6

Recently, I've discovered I really like jQuery.Deferred() and the features it gives you to handle asynchronous flow control (Promises). I suppose the things I like the most ar the callback hooks for an Ajax request (.done() and .fail() ) and ability to .resolve() and .reject() a promise. I might need this functionality at some point for a GWT project and I was wondering, is there anything analogous in that ecosystem?

Taliped answered 20/2, 2013 at 17:32 Comment(0)
N
8

gwtquery 1.4.0 implements Deferred in the same way jquery does:

  • They are Based on the CommonJS Promises/A+ spec
  • The gQuery implementation is inspired in the jQuery API.
  • And gQuery promises are MVP compliant, so as they can be run in the JVM.

Recently, we have given a gwtquery presentation in the GWT.create Conference introducing new stuff like promises, you can take a look to the slides (push arrow keys to move between slides).

ajax() now returns a Promise, you can as well get the promise() associated with the queue of any gQuery object. Additionally you can create Deferred() in any gwt callback or use helper functions to deal with RequestBuider, RPC, RF.

Taken from the junit tests, here you have a portion of code of how can you use them in your code:

  public void testDeferredAjaxWhenDone() {
    String url = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY";

    delayTestFinish(5000);
    GQuery.when(Ajax.getJSONP(url))
      .done(new Function() {
        public void f() {
          Properties p = arguments(0);
          assertEquals(400, p.getProperties("error").getInt("code"));
          finishTest();
        }
      });
  }

  public void testDeferredAjaxWhenFail() {
    String url1 = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY";
    String url2 = "https://localhost:4569/foo";

    delayTestFinish(5000);
    GQuery.when(
     Ajax.getJSONP(url1), 
     Ajax.getJSONP(url2))
      .done(new Function() {
        public void f() {
          fail();
        }
      })
      .fail(new Function(){
        public void f() {
          finishTest();
        }
      });
  }

  int progress = 0;
  public void testPromiseFunction() {
    delayTestFinish(3000);
    final Promise doSomething = new PromiseFunction() {
      @Override
      public void f(final Deferred dfd) {
        new Timer() {
          int count = 0;
          public void run() {
            dfd.notify(count ++);
            if (count > 3) {
              cancel();
              dfd.resolve("done");
            }
          }
        }.scheduleRepeating(50);
      }
    };

    doSomething.progress(new Function() {
      public void f() {
        progress = arguments(0);
      }
    }).done(new Function() {
      public void f() {
        assertEquals(3, progress);
        assertEquals(Promise.RESOLVED, doSomething.state());
        finishTest();
      }
    });
  }
Netta answered 20/2, 2013 at 19:4 Comment(0)
S
6

I've just open-sourced Promises for Java inspired by the CommonJS Promises/A+ proposal and Dart's Futures.

My goal is to add GWT support soon (GWT compatibility for Promises and FulfillablePromise, and helpers for, at least, GWT-RPC).

Stoddart answered 28/2, 2013 at 13:51 Comment(3)
Nice! I'll give it a try soon :)Taliped
throyer/promises has been deprecated. Any guidance on where to go?Hartmunn
gwtquery or JsInteropStoddart

© 2022 - 2024 — McMap. All rights reserved.