并发限制k版本,牛客咋不让贴代码了?? class Scheduler { constructor(limit) { this.queue = []; this.limit = limit; this.currentTaskCount = 0; } add(fn) { return new Promise((resolve, reject) => { this.queue.push([fn, resolve]); this.exc(); }) } exc() { if (this.currentTaskCount < this.limit && this.queue.length) { const [fn, resolve] = this.queue.shift(); this.currentTaskCount++; Promise.resolve(fn()).then((result) => { resolve(result); this.currentTaskCount--; this.exc(); }) } } }
2 1

相关推荐

1.&nbsp;看代码说输出,以及说出如何让输出变为1,2,3,4,5```&nbsp;tsfor(var&nbsp;i=0;i&lt;5;++i){console.log(i)}```2.&nbsp;React组件先触发add,再点击btn说输出,再说如何让点击btn只输出2```&nbsp;tsxfunction&nbsp;App(){const&nbsp;[count,setCount]&nbsp;=&nbsp;useState(1)useEffect(()=&gt;{document.querySelector('#btn').addEventListener('click',()=&gt;{console.log(count)})})return&nbsp;&lt;div&gt;&lt;div&nbsp;onClick={()=&gt;setCount(p=&gt;p+1)}&gt;&lt;/div&gt;&lt;div&nbsp;id=&quot;btn&quot;&gt;log&lt;/div&gt;&lt;/div&gt;}```3.&nbsp;说说对闭包的理解,闭包的原理4.&nbsp;项目5.&nbsp;实习6.&nbsp;手写异步控制队列,```&nbsp;tsclass&nbsp;Scheduler&nbsp;{add()&nbsp;{//&nbsp;写}}const&nbsp;scheduler&nbsp;=&nbsp;new&nbsp;Scheduler(2);scheduler.add(()&nbsp;=&gt;&nbsp;new&nbsp;Promise((resolve)&nbsp;=&gt;&nbsp;setTimeout(()&nbsp;=&gt;&nbsp;resolve(1),&nbsp;2000))).then(console);scheduler.add(()&nbsp;=&gt;&nbsp;new&nbsp;Promise((resolve)&nbsp;=&gt;&nbsp;setTimeout(()&nbsp;=&gt;&nbsp;resolve(2),&nbsp;300))).then(console);scheduler.add(()&nbsp;=&gt;&nbsp;new&nbsp;Promise((resolve)&nbsp;=&gt;&nbsp;setTimeout(()&nbsp;=&gt;&nbsp;resolve(3),&nbsp;800))).then(console);scheduler.add(()&nbsp;=&gt;&nbsp;new&nbsp;Promise((resolve)&nbsp;=&gt;&nbsp;setTimeout(()&nbsp;=&gt;&nbsp;resolve(4),&nbsp;1000))).then(console);```二面面试官一直在笑,态度好的不得了,估计是因为我一面的时候吹逼说“我喜欢用数据结构解决问题“,然后一面面试官说”我们有个同事也喜欢,或许你和他聊得来“,估计是这位面试官了,哭了兄弟们,太和蔼了明天晚上8点三面
查看6道真题和解析
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务