question/implement-promise-all #35
Replies: 2 comments 1 reply
-
/**
* @param {Array<any>} promises - notice input might have non-Promises
* @return {Promise<any[]>}
*/
function all(promises) {
return new Promise((resolve, reject) => {
let len = promises.length;
const result = [];
if (promises.length === 0) {
resolve(result);
}
promises.forEach((p, i) => {
(p && p.then ? p : Promise.resolve(p))
.then(val => {
result[i] = val;
if (--len === 0) {
resolve(result);
}
})
.catch(reject);
})
});
}
all([Promise.resolve(1), 2])
.then(r => console.log(r))
.catch(r => console.log(r)); |
Beta Was this translation helpful? Give feedback.
1 reply
-
这个题目本身还是比较简单的 []
[1,2,3,4]
"error" |
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-all
北美前端面试攻略
https://us-fe.github.io/question/implement-promise-all.html
Beta Was this translation helpful? Give feedback.
All reactions