diff --git a/doc/api/process.md b/doc/api/process.md index 1eff6a5b261abc..e0ab670747516b 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -457,6 +457,15 @@ of the custom deprecation. The `*-deprecation` command-line flags only affect warnings that use the name `'DeprecationWarning'`. +### Event: `'worker'` + + +* `worker` {Worker} The {Worker} that was created. + +The `'worker'` event is emitted after a new {Worker} thread has been created. + #### Emitting custom warnings See the [`process.emitWarning()`][process_emit_warning] method for issuing diff --git a/lib/internal/worker.js b/lib/internal/worker.js index f1da0d4ded4cea..f2414ebeec4aae 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -266,6 +266,8 @@ class Worker extends EventEmitter { }; // Actually start the new thread now that everything is in place. this[kHandle].startThread(); + + process.nextTick(() => process.emit('worker', this)); } [kOnExit](code, customErr, customErrReason) { diff --git a/test/parallel/test-worker-event.js b/test/parallel/test-worker-event.js new file mode 100644 index 00000000000000..9d6056e1091468 --- /dev/null +++ b/test/parallel/test-worker-event.js @@ -0,0 +1,13 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { + Worker, +} = require('worker_threads'); + +process.on('worker', common.mustCall(({ threadId }) => { + assert.strictEqual(threadId, 1); +})); + +new Worker('', { eval: true });