BraveNewTalent
  • Sign in Join now
  • Phil Booth

    8th April 2012 Phil Booth in Javascript

    When making asynchronous HTTP requests from javascript, it can be very easy for code to descend into a tightly coupled mess of spaghetti as more and more requests are added.


    jQuery has some good, under-appreciated functions that help you to apply patterns that keep the codebase clean. They're members of the jQuery `Deferred` object and the javascript playground has a short beginner-level tutorial, which is an easier introduction to using them than the official jQuery documentation.


    You can read the tutorial here:


    http://javascriptplayground.com/blog/2012/04/jquery-deferreds-tutorial
    • An introduction to jQuery Deferreds | Blog :: The JavaScript Playground
      An introduction to jQuery Deferreds | Blog :: The JavaScript Playground

      Prior to jQuery 1.5, Ajax requests could get a bit messy. You'd probably do something like this, if we were making a simple get request: $(function() { $.get( "content.txt", function(resp) { console.log("first code block"); console.log(resp); } ); }); But this gives you a potential issue - what happens if this fails?

    2 people liked this
    • Like
    • Comment
    • Share
      • Facebook
      • Twitter
      • LinkedIn
      • Google+
    • Phil Booth
      Phil Booth
      If you're not using jQuery but are interested in the subject of deferreds/futures/promises as a pattern for asynchronous programming in javascript, there's a library called Q that implements them:


      https://github.com/kriskowal/q


      Does anyone have other recommendations in this area?
      • 8th April 2012
      • Like
    Phil Booth

    Phil Booth

    JavaScript Guy

    London, GB
    69 Followers 19 Following 1 Organization 20 Topics

    Follow Phil Booth for more content like this.

    • © 2011-2013 BraveNewTalent
    • For Enterprise
    • About
    • Team
    • Legal
    • Contact
    • Sitemap