Skip to content

Instantly share code, notes, and snippets.

@piecyk
Last active December 5, 2015 08:20
Show Gist options
  • Select an option

  • Save piecyk/40b1ac8ae76c663b495f to your computer and use it in GitHub Desktop.

Select an option

Save piecyk/40b1ac8ae76c663b495f to your computer and use it in GitHub Desktop.
function resolvePending(arrayOfPromises) {
const d = $.Deferred();
let resolved = [];
let rejected = [];
const check = () => {
if (resolved.length + rejected.length === arrayOfPromises.length) {
rejected.length > 0 ? d.reject(rejected) : d.resolve(resolved);
}
};
$.each(arrayOfPromises, function(i, fn) {
fn().
done(res => {
resolved = resolved.concat(res);
check();
}).
fail(res => {
rejected = rejected.concat(res);
check();
});
});
return d;
}
function asyncThing() {
//return promise
}
resolvePending([asyncThing, asyncThing, asyncThing]).
then(function(resolved) {
console.log(resolved);
}).
fail(function(rejected) {
console.log(rejected);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment