Skip to content

Instantly share code, notes, and snippets.

@libook
Created August 14, 2019 08:51
Show Gist options
  • Save libook/7a9743cadc14fa4821802c12b523be2e to your computer and use it in GitHub Desktop.
Save libook/7a9743cadc14fa4821802c12b523be2e to your computer and use it in GitHub Desktop.
Polyfill of Promise.any
/**
* Polyfill of Promise.any
* @param {Promise[]} promiseList
* @returns {Promise<Object>} - First fulfilled result form one of promiseList.
* @throws {Error} - Provide mixed error message and errors property.
*/
const promiseAny = async (promiseList) => {
return Promise.all(promiseList.map(promise => {
return promise.then(
value => {
throw value;
},
error => error,
);
})).then(
(errorList) => {
const mixedError = new Error(
errorList.reduce(
(message, error) => {
return message += `\n` + error.message;
},
'Mixed Error: ',
),
);
mixedError.errors = errorList;
throw mixedError;
},
result => result,
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment