Skip to content

Instantly share code, notes, and snippets.

@nikhilw
Created May 17, 2016 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhilw/fd1429064c14ab6c086cca7ef99ecd43 to your computer and use it in GitHub Desktop.
Save nikhilw/fd1429064c14ab6c086cca7ef99ecd43 to your computer and use it in GitHub Desktop.
(function($, window, undefined) {
function AsyncEachHandler(arr, it, done) {
var defs = [];
$.each(arr, function(i, item) {
var def = new $.Deferred();
defs.push(def);
it(item, function(err) {
console.log("in here")
if (err) {
def.reject();
}
def.resolve();
});
});
$.when.apply($, defs).done(done);
}
$.fn.asyncEach = function(arr, it, done) {
new AsyncEachHandler(arr, it, done);
return this;
}
$.asyncEach = {version : "0.1"}
})(jQuery, window);
// How to use
$.fn.asyncEach([ 1, 2, 3, 4, 5, 6 ], function(item, callback) {
console.log("got me items: ", item, callback)
setTimeout(function() {
console.log("resolved for: " + item);
callback();
}, item * 1000)
}, function() {
console.log("done all!")
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment