Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save juandopazo/9785838 to your computer and use it in GitHub Desktop.
Save juandopazo/9785838 to your computer and use it in GitHub Desktop.
CancellablePromise.all = function (values) {
var CancellablePromise = this;
var child;
var children = [];
return new CancellablePromise(function (resolve, reject) {
if (!A.Lang.isArray(values)) {
reject(new TypeError('CancellablePromise.all expects an array of values or promises'));
return;
}
var remaining = values.length,
i = 0,
length = values.length,
results = [];
function oneDone(index) {
return function (value) {
results[index] = value;
remaining--;
if (!remaining) {
resolve(results);
}
};
}
if (length < 1) {
return resolve(results);
}
for (; i < length; i++) {
child = values[i];
child.then(oneDone(i), reject);
children.push(child);
}
}).then(function() {
console.log('CancellablePromise resolved');
},
function(reason) {
console.log('CancellablePromise rejected', children);
throw reason;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment