Skip to content

Commit cb68e73

Browse files
committed
5.2
1 parent 1a7165c commit cb68e73

File tree

1 file changed

+16
-7
lines changed
  • 05-asynchronous-control-flow-patterns-with-promises-and-async-await/05-promises-web-spider-v4

1 file changed

+16
-7
lines changed

05-asynchronous-control-flow-patterns-with-promises-and-async-await/05-promises-web-spider-v4/TaskQueue.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,32 @@ export class TaskQueue {
55
this.queue = []
66
}
77

8+
/**
9+
* @param {() => Promise<any>} task
10+
*/
811
runTask (task) {
912
return new Promise((resolve, reject) => {
10-
this.queue.push(() => {
11-
return task().then(resolve, reject)
13+
this.queue.push(async () => {
14+
try {
15+
resolve(await task())
16+
} catch (error) {
17+
reject(error)
18+
}
1219
})
1320
process.nextTick(this.next.bind(this))
1421
})
1522
}
1623

17-
next () {
24+
async next () {
1825
while (this.running < this.concurrency && this.queue.length) {
1926
const task = this.queue.shift()
20-
task().finally(() => {
21-
this.running--
22-
this.next()
23-
})
2427
this.running++
28+
try {
29+
await task();
30+
} finally {
31+
this.running--
32+
this.next();
33+
}
2534
}
2635
}
2736
}

0 commit comments

Comments
 (0)