question/implement-promise-any #37
Replies: 1 comment
-
/**
* @param {Array<Promise>} promises
* @return {Promise}
*/
function any(promises) {
// your code here
return new Promise((resolve, reject) => {
const errors = [];
let len = promises.length;
const throwError = () => {
reject(new AggregateError(
'No Promise in Promise.any was resolved',
errors
))
}
if (len == 0) {
throwError();
}
promises.forEach((p, i) => {
Promise.resolve(p)
.then(resolve)
.catch((e) => {
errors[i] = e;
if (--len === 0) {
throwError();
}
})
})
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
question/implement-promise-any
北美前端面试攻略
https://us-fe.github.io/question/implement-promise-any.html
Beta Was this translation helpful? Give feedback.
All reactions