Chaining jQuery AJAX Calls (w/o Plugins)

Here’s the scenario: you need to make a series of AJAX calls to process a list of objects and each call is dependent on the results from the previous call.  How can we structure this elegantly in jQuery without having to write massive chains or script callbacks?

An example would be using an AJAX based API to create a hierarchy of nested folders.  Perhaps the user enters a string like “/path1/path2/path3” and multiple calls are needed to create “/path1″, then /path1/path2,” then “/path1/path2/path3”.

My friend John Peterson brought up jQuery deferred to me today and it clicked and my life is better for it.

Here’s an example:

The general idea is that we start with a chain and “pipe” the output from each step to the next.  When I execute the chain, I pass in the array as the argument on line 35.

This is necessary because referring to part[i] inside the function declared on line 11 will always yield “part3” .  To work around this, we pass in the whole array of parts as the context to the chain.  It becomes the “this” reference and we simply shift() a value off of the array to get the “current” item.

To pass the array to the next function in the chain, we simply set the context of the AJAX call to the “this” reference.  Now, in the next handler in the chain, the “response” is the output of the previous AJAX call and the “this” reference is the “current” item in the array.

Of course, the final call — if you want to handle it — can be added to the chain using done() instead of pipe().

It’s a brilliant way of chaining multiple jQuery AJAX calls in a much more coherent manner!

You may also like...

1 Response

  1. August 24, 2011

    […] can be downloaded here * Deferred Objects documentation * Fun With jQuery Deferred blog post * Chaining jQuery AJAX Calls (w/o Plugins) blog postRelated posts:jQuery Ajax Memory Leak in IE8Mobile API Requests with JSONFinding an […]