Skip to content

Instantly share code, notes, and snippets.

@ostgals ostgals/snippet01.js
Last active Oct 10, 2015

Embed
What would you like to do?
Snippet - Multi-thread queue with jq's promises
{
// .....
enqueue: function(items, get_promise, num_threads){
num_threads = num_threads || 1;
var dfd = $.Deferred(), i = 0, num_total = items.length, num_active = 0, num_done = 0,
next = function(){
while (num_active < num_threads && i < num_total) {
num_active++;
get_promise(items[i++]).done(function(){
num_active--;
dfd.notify(++num_done, num_total);
if (num_done == num_total) dfd.resolve();
next();
});
});
};
next();
return dfd.promise();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.