尝试一下,如有错误请大佬指正! async function parallel(tasks, n) { const ans = {resolved: [], rejected: []}; const executing = new Set(); for (const task of tasks) { const e = task().then(res => { executing.delete(e); ans.resolved.push(res); }).catch(err => { executing.delete(e); ans.rejected.push(err); }); executing.add(e); if (executing.size >= n) { await Promise.race(executing); } } while (executing.size > 0) { await Promise.race(executing); } return ans; }
点赞 2

相关推荐

10-16 15:48
算法工程师
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务