jquerygwtpromisejquery-deferredgwtquery

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?


Solution

  • gwtquery 1.4.0 implements Deferred in the same way jquery does:

    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();
          }
        });
      }