Created
December 28, 2013 10:54
-
-
Save pc035860/8158249 to your computer and use it in GitHub Desktop.
Chunked Executions
Need "setImmediate" and "Promise"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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