Hybrid View

  1. #1
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,321
    Vote Rating
    114
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default Promises and Deferreds

    Promises and Deferreds


    It would be nice to have Promises and Deferreds added to Ext JS.

    Promisses are used extensively in jQuery and Dojo. Also, DeftJS uses them as well.

    See the sample chapter from the Async JavaScript book which explains why Promises and Deferreds are helpful.

    https://leanpub.com/asyncjs

    http://samples.leanpub.com/asyncjs-sample.pdf

    http://deftjs.org/

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    37,004
    Vote Rating
    846
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Way out of scope for 4.x. Personally I have not been sold on the promises vs how things are done today. Like saying, "what's wrong with how it's done currently?"
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,321
    Vote Rating
    114
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    Way out of scope for 4.x. Personally I have not been sold on the promises vs how things are done today. Like saying, "what's wrong with how it's done currently?"
    The existing Ext JS API is not convenient for some tasks. For example, when you have a set of async tasks, it's not easy to find out when these tasks completed. This is where you would use Ext.when() and Ext.then(). See the Dojo, jQuery or DeftJS implementation.

    http://api.jquery.com/jQuery.when/
    Last edited by LesJ; 5 Nov 2012 at 6:39 AM. Reason: add deftJS reference

  4. #4
    Sencha User debianw's Avatar
    Join Date
    Mar 2012
    Location
    Costa Rica
    Posts
    49
    Vote Rating
    2
    debianw is on a distinguished road

      0  

    Default


    I agree. Promises pattern help us to clean the code of nested pyramid callbacks.

    In asynchronous programing sometimes We end with something like:
    Code:
    doSomething(function(foo){
      foo = processFoo(foo);
      doMoreStuff(foo, function(bar){
        var baz = getBaz(foo);
        stillMore(baz, function(quux){
          gettingPrettyBad(function(widget){
            return widget + baz;
          });
        });
      });
    });
    Extracted from a link regarding promises:
    "Callbacks are a very useful and tremendously powerful tool. They also have some oddities and quirks related to the “this” variable (JavaScript’s context), but that can easily be managed. But worse than managing “this”, callbacks have a much deeper problem relating to code readability and maintainability"

  5. #5
    Sencha User
    Join Date
    Dec 2009
    Location
    Enschede, The Netherlands
    Posts
    327
    Vote Rating
    13
    flanders will become famous soon enough

      0  

    Default


    To be honest I do not really see the issue to. Whats the advantage over:

    Code:
    doSomething: function(foo) {
      this.doMoreStuff(foo, this.doEvenMoreStuff);
    },
    
    doMoreStuff: function(foo, cb) {
      ...
      cb();
    },
    
    doEvenMoreStuff: function(bar) {
      doFinalStuff(bar, this.doReallyFinalStuff);
    },
    
    doFinalStuff: function(we, cb) {
      ...
      cb();
    },
    
    doReallyFinalStuff: function(bar, cb) {
      ...
      cb();
    },
    It might be a minor convenience when wanting to act on to events having occured, but then we can do:

    Code:
    onDataStructureReceived: function(structure) {
      ..
      this.structureProcessed = true;
      this.checkReadyToProceed();
    },
    
    onDataReceived: function(data) {
      ...
      this.dataProcessed = true;
      this.checkReadyToProceed();
    },
    
    checkReadyToProceed: function() {
      if (this.structureProcessed && this.dataProcessed) {
        doStuff();
      }
    },
    
    start: function() {
      doAjaxCall(url, onDataStructureReceived);
      doAjaxCall(anotherUrl, onDataReceived);
    }
    So I see no benefit at all for the sequential case. Actually I find listing all methods to an object and using them where appropiate more readable. I see a few lines of code saved in cases where you want to wait on multiple conditions. However, I feel that a lot of convenience wrappers like this add more unnecessary complexity then yield positive results.

    What if I want to act on the first returning call or when I have received 2 out of 3?

  6. #6
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,321
    Vote Rating
    114
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    flanders, you assumed that all responses will be successful. This may or may not be the case. If you wanted to handle failures, your workaround code would be quite messy. The code with promisses is much simpler.

    Also, keep in mind that promisses can be passed around which aids encapsulation.

  7. #7
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,321
    Vote Rating
    114
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    One step closer to moving Futures (nee "Promises") into DOM + JS:

    A design for Futures/Promises in DOM

    http://lists.w3.org/Archives/Public/...nMar/0200.html