Skip to content

Instantly share code, notes, and snippets.

@dtipson
Created February 10, 2012 16:46
Show Gist options
  • Select an option

  • Save dtipson/1790763 to your computer and use it in GitHub Desktop.

Select an option

Save dtipson/1790763 to your computer and use it in GitHub Desktop.
bit of code (part of a larger object not shown) that handles batching of a huge CORS POST into several JSONP requests. The rwJSONP function returns a promise
batchIt: function(q){
var b = $.Deferred(),
batches = [],
per=3,
nq,
nids=[];
$.each(q.ids,function(i,v){
nids.push(v);
if (nids.length===per) {
nq = $.extend({}, q);
nq.ids = nids;
batches.push(hashC.rwJSONP(nq));
nids = [];
}
});
$.when.apply(this,batches).then(function(){
//need to merge all the arguments back together into a single result, oy
var result ={};
$.each(arguments,function(i,v){
$.extend(result,v[0]);
});
b.resolve(result);
},function(){
//one of the batches failed. darn
b.reject();
});
return b.promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment