* @param {Array<any>} promises - notice input might have non-Promises
* @return {Promise<any[]>}
*/
async function all(promises) {
// your code here
const result = []
for await (let res of promises)
result.push(res)
return result
}
/**
* @param {Array<any>} promises - notice input might have non-Promises
* @return {Promise<any[]>}
*/
async function all(promises) {
const results = [];
for (let promise of promises) {
results.push(await promise);
}
return results;
}
- zhuanlan.zhihu.com/p/41502945 基于promise的版本,该链接提供的实现有问题,但思路是对的,先放这里,以后完善
demo:
let resList = [1,2,3,4,5].map((id)=>{
return fetch(`https://jsonplaceholder.typicode.com/users/${id}`).then(res => res.json())
})
async function all(promises) {
const results = [];
for (let promise of promises) {
results.push(await promise);
}
return results;
}
all(resList).then(console.log)