function runAll(functions, onSuccess, onError) {
var results = [];
var overallError;
functions.forEach(function (func, index) {
function handleFuncSuccess(data) {
results[index] = data;
}
function handleFuncError(error) {
overallError = error;
break;
}
func(handleFuncSuccess, handleFuncError);
});
if (overallError) {
onError(overallError);
} else {
onSuccess(results);
}
}
function func1(onSuccess) { onSuccess('great success!') }
function func2(onSuccess) { onSuccess('yes, very nice') }
runAll([func1, func2], function (data) {
data === [
'great success!',
'yes, very nice'
]
});
function func1(onSuccess) { onSuccess('great success!') }
function func2(onSuccess, onFailure) { onFailure('fail') }
runAll([func1, func2], null, function (error) {
error === 'fail';
});
Promise.all([
Promise.resolve('great success!'),
Promise.resolve('yes, very nice')
]).then(data => {
})
Promise.all([
Promise.resolve('great success!'),
Promise.reject('fail')
]).catch(error => {
})