Hacker Newsnew | comments | show | ask | jobs | submit login

> "This is better, it definitely makes it clearer what exactly is going on"

    function createItem(item, successCallback, errorCallback) {
      var req = $.post("/api/item", item);
      req.success(successCallback);
      req.error(errorCallback);
    }
How is this more clear than the default jQuery $.post?



Not really better. The advantages of promises show up when you pass promises around, letting other people add their callbacks to it (this way the "callback structure" is implicit from the program flow, just like what happens when you do normal sync programming. Another advantage of promises is that they have some better support for error handling (you don't need to thread the error handler around and you "throw" statements are caught correctly)

Honestly, I didn't like the examples in the article very much. Not only do they fail to show the major advantages promises have, but he also uses named functions everywhere and that is quite silly.

-----


The named functions were just an attempt at better highlighting what is going on for some readers. Not sure if it worked, but sorry it annoyed you :).

-----


I personally find that creating too many functions makes the code look too different from the "natural", synchronous alternative. But I also hate when people write tons of 1-line methods (Uncle Bob style) so YMMV.

-----


Agh, this is an editing mistake. I re-worded this last minute and reading again next day, it was a mistake.

Really what I meaning to say is that it's much clearer as to what is happening. I was trying to point out that success and error were actually functions on the deferred returned from $.post, not just functions you can pass into $.post (as they once were - hence "circa 2009").

-----




Guidelines | FAQ | Support | API | Lists | Bookmarklet | DMCA | Y Combinator | Apply | Contact

Search: