Skip to content

Instantly share code, notes, and snippets.

@baconpat
Created August 24, 2011 16:08
Show Gist options
  • Save baconpat/1168407 to your computer and use it in GitHub Desktop.
Save baconpat/1168407 to your computer and use it in GitHub Desktop.
JavaScript function for making sequential ajax requests.
// Makes sequential getJSON requests, passing the processed results from the
// previous request to the next request. seedData is optional.
var chainedGetJSON = function(requests, seedData) {
var seed = $.Deferred(),
finalPromise;
finalPromise = requests.reduce(function(promise, request) {
return promise.pipe(function(input) {
return $.getJSON(request.url(input)).pipe(function(json) {
return request.process(json, input);
});
});
}, seed.promise());
// Start the chain
seed.resolve(seedData);
return finalPromise;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment