| 474 | } |
| 475 | |
| 476 | terminate() { |
| 477 | debug(`[${threadId}] terminates Worker with ID ${this.threadId}`); |
| 478 | |
| 479 | this.ref(); |
| 480 | |
| 481 | if (this[kHandle] === null) return PromiseResolve(); |
| 482 | |
| 483 | this[kHandle].stopThread(); |
| 484 | |
| 485 | // Do not use events.once() here, because the 'exit' event will always be |
| 486 | // emitted regardless of any errors, and the point is to only resolve |
| 487 | // once the thread has actually stopped. |
| 488 | const { promise, resolve } = PromiseWithResolvers(); |
| 489 | this.once('exit', resolve); |
| 490 | return promise; |
| 491 | } |
| 492 | |
| 493 | async [SymbolAsyncDispose]() { |
| 494 | await this.terminate(); |