diff --git "a/04-Promise\357\274\232\345\274\202\346\255\245\345\271\266\345\217\221\351\231\220\345\210\266\345\231\250/01_test.js" "b/04-Promise\357\274\232\345\274\202\346\255\245\345\271\266\345\217\221\351\231\220\345\210\266\345\231\250/01_test.js" index 712ea75..6622c5f 100644 --- "a/04-Promise\357\274\232\345\274\202\346\255\245\345\271\266\345\217\221\351\231\220\345\210\266\345\231\250/01_test.js" +++ "b/04-Promise\357\274\232\345\274\202\346\255\245\345\271\266\345\217\221\351\231\220\345\210\266\345\231\250/01_test.js" @@ -11,17 +11,19 @@ class Scheduler { add(promiseCreater) { let _resolve; - this.queue.push(promiseCreater); + const pro = new Promise(resolve => _resolve = resolve); + this.queue.push({ p: promiseCreater, r: _resolve}); const doNext = () => { // 队列中存在任务,并且当前正在执行的任务数量小于最大值 debugger - if (this.queue.length && this.count < this.maxCount) { + if (this.queue.length && this.queue.length < this.maxCount) { this.count++; - this.queue.shift()().then(() => { + const task = this.queue.shift(); + task.p().then(() => { // 任务执行结束,输出结果 debugger - _resolve(123); // 存在 resolve之后没有输出的情况 + task.r(123); // 存在 resolve之后没有输出的情况 debugger this.count--; @@ -32,9 +34,7 @@ class Scheduler { doNext(); - return new Promise((resolve) => { - _resolve = resolve; - }) + return pro; } } @@ -57,4 +57,4 @@ addTask(4000, '3') debugger addTask(4000, '4') debugger -// 结果: 2 3 1 4 \ No newline at end of file +// 结果: 2 3 1 4