From a21f9e11a86f3ba632bbd590735981a64dd66a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E9=A1=BA=E5=90=8D=28Jarvis=29?= Date: Thu, 8 Jul 2021 14:47:17 +0800 Subject: [PATCH 1/2] Update 01_test.js --- .../01_test.js" | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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..a50cf9f 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" @@ -16,7 +16,7 @@ class Scheduler { const doNext = () => { // 队列中存在任务,并且当前正在执行的任务数量小于最大值 debugger - if (this.queue.length && this.count < this.maxCount) { + if (this.queue.length) { this.count++; this.queue.shift()().then(() => { // 任务执行结束,输出结果 @@ -30,7 +30,9 @@ class Scheduler { } } - doNext(); + if (this.queue.length < this.maxCount) { + doNext(); + } return new Promise((resolve) => { _resolve = resolve; @@ -57,4 +59,4 @@ addTask(4000, '3') debugger addTask(4000, '4') debugger -// 结果: 2 3 1 4 \ No newline at end of file +// 结果: 2 3 1 4 From b8849f30775625e02b60dc291130540b6895986f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E9=A1=BA=E5=90=8D=28Jarvis=29?= Date: Thu, 8 Jul 2021 15:05:59 +0800 Subject: [PATCH 2/2] Update 01_test.js --- .../01_test.js" | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) 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 a50cf9f..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) { + 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--; @@ -30,13 +32,9 @@ class Scheduler { } } - if (this.queue.length < this.maxCount) { - doNext(); - } + doNext(); - return new Promise((resolve) => { - _resolve = resolve; - }) + return pro; } }