Created
August 17, 2017 09:17
-
-
Save AldoMX/4233a8c1d02732a489adc667691c5c6c 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
/*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