Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.Deferred() / Promises functionality in GWT?

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?

like image 274
blong Avatar asked Feb 20 '13 17:02

blong


People also ask

How use JQuery Deferred and Promise?

If you are creating a Deferred, keep a reference to the Deferred so that it can be resolved or rejected at some point. Return only the Promise object via deferred. promise() so other code can register callbacks or inspect the current state. For more information, see the documentation for Deferred object.

What is JQuery Deferred and Promise object?

A deferred object is an object that can create a promise and change its state to resolved or rejected . Deferreds are typically used if you write your own function and want to provide a promise to the calling code. You are the producer of the value. A promise is, as the name says, a promise about future value.

What is Deferred function in JQuery?

Deferred() A factory function that returns a chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.


2 Answers

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();
      }
    });
  }
like image 57
Manolo Carrasco Moñino Avatar answered Oct 01 '22 06:10

Manolo Carrasco Moñino


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).

like image 39
Thomas Broyer Avatar answered Oct 01 '22 05:10

Thomas Broyer