Queued Ajax Requests With jQuery and Coffescript

I recently had to queue a few ajax requests. I wrote a little piece of Coffeescript+jQuery to provide a reusable way of doing it. Using jQuery’s Deferred objects and CoffeeScript makes the task really easy.

You probably already know that all jQuery’s ajax methods implement Promise interface. That’s why we can use the pipe() method to queue the queries, as well as done() and fail() methods to track the queries’ progress.

Another interesting point is how CoffeeScript make the code clearer and easy to read. I particularly enjoyed using splats (...) to write the $.when(deferredObjs...) part on line 34. Speaking about code readability, it’s such an improvement compared to the $.when.apply($, deferredObjs) Javascript counterpart.

You can find a working sample on jsFiddle.

References

Comments