Skip to content

Instantly share code, notes, and snippets.

@AldoMX
Created August 17, 2017 09:17
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 AldoMX/4233a8c1d02732a489adc667691c5c6c to your computer and use it in GitHub Desktop.
Save AldoMX/4233a8c1d02732a489adc667691c5c6c to your computer and use it in GitHub Desktop.
/*export default*/ module.exports = (callbacks, timeout = 0) => {
callbacks = callbacks.reverse();
return new Promise((resolve, reject) => {
const values = [];
const errors = [];
let hasError = false;
(function nextPromise() {
let currentCallback = callbacks.pop();
if (currentCallback) {
currentCallback()
.then(value => {
values.push(value);
errors.push(undefined);
})
.catch(err => {
hasError = true;
values.push(undefined);
errors.push(err);
})
.then(() => setTimeout(() => nextPromise(), timeout));
}
else if (hasError) {
reject({ values: values, errors: errors });
}
else {
resolve(values);
}
})();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment