-
-
Save juandopazo/9785838 to your computer and use it in GitHub Desktop.
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
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