diff --git a/doc/api/async_hooks.md b/doc/api/async_hooks.md index 99b8f038ddf211..6abc66bfb8d815 100644 --- a/doc/api/async_hooks.md +++ b/doc/api/async_hooks.md @@ -833,9 +833,19 @@ class WorkerPool extends EventEmitter { this.numThreads = numThreads; this.workers = []; this.freeWorkers = []; + this.tasks = []; for (let i = 0; i < numThreads; i++) this.addNewWorker(); + + // Any time the kWorkerFreedEvent is emitted, dispatch + // the next task pending in the queue, if any. + this.on(kWorkerFreedEvent, () => { + if (this.tasks.length > 0) { + const { task, callback } = this.tasks.shift(); + this.runTask(task, callback); + } + }); } addNewWorker() { @@ -869,7 +879,7 @@ class WorkerPool extends EventEmitter { runTask(task, callback) { if (this.freeWorkers.length === 0) { // No free threads, wait until a worker thread becomes free. - this.once(kWorkerFreedEvent, () => this.runTask(task, callback)); + this.tasks.push({ task, callback }); return; }