Skip to content

Instantly share code, notes, and snippets.

@pc035860
Created December 28, 2013 10:54
Show Gist options
  • Save pc035860/8158249 to your computer and use it in GitHub Desktop.
Save pc035860/8158249 to your computer and use it in GitHub Desktop.
Chunked Executions Need "setImmediate" and "Promise"
// async version of timedChunk plus queued by robin
function asyncChunkQueued(items, process, context, callback) {
var todo = items.concat(), //create a clone of the original
i = 0, // index counter
dfd = $.Deferred(),
routine = function () {
process.call(context, todo.shift(), i++)
.then(function () {
if (todo.length > 0) {
_setImmediate(routine);
}
else {
if (typeof callback !== 'undefined') {
callback(items);
}
dfd.resolve(items);
}
});
};
_setImmediate(routine);
return dfd.promise();
}
//Copyright 2009 Nicholas C. Zakas. All rights reserved.
//MIT Licensed
// modified by robin
function timedChunk(items, process, context, callback) {
var todo = items.concat(), //create a clone of the original
i = 0, // index counter
dfd = $.Deferred();
_setImmediate(function () {
var start = +new Date();
do {
process.call(context, todo.shift(), i++);
} while (todo.length > 0 && (+new Date() - start < 50));
if (todo.length > 0){
_setImmediate(arguments.callee);
} else {
if (typeof callback !== 'undefined') {
callback(items);
}
dfd.resolve(items);
}
});
return dfd.promise();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment