From f6f95bf61e84dae44b16c51ebb4a9c5b46194583 Mon Sep 17 00:00:00 2001 From: OneNail Date: Tue, 3 May 2022 05:24:13 +0800 Subject: doc: fix examples in cluster.md PR-URL: https://github.com/nodejs/node/pull/42889 Reviewed-By: Antoine du Hamel Reviewed-By: Mohammed Keyvanzadeh --- doc/api/cluster.md | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'doc') diff --git a/doc/api/cluster.md b/doc/api/cluster.md index ce38048678c..1f7cfed56fc 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -197,31 +197,35 @@ Similar to the `cluster.on('exit')` event, but specific to this worker. ```mjs import cluster from 'node:cluster'; -const worker = cluster.fork(); -worker.on('exit', (code, signal) => { - if (signal) { - console.log(`worker was killed by signal: ${signal}`); - } else if (code !== 0) { - console.log(`worker exited with error code: ${code}`); - } else { - console.log('worker success!'); - } -}); +if (cluster.isPrimary) { + const worker = cluster.fork(); + worker.on('exit', (code, signal) => { + if (signal) { + console.log(`worker was killed by signal: ${signal}`); + } else if (code !== 0) { + console.log(`worker exited with error code: ${code}`); + } else { + console.log('worker success!'); + } + }); +} ``` ```cjs const cluster = require('node:cluster'); -const worker = cluster.fork(); -worker.on('exit', (code, signal) => { - if (signal) { - console.log(`worker was killed by signal: ${signal}`); - } else if (code !== 0) { - console.log(`worker exited with error code: ${code}`); - } else { - console.log('worker success!'); - } -}); +if (cluster.isPrimary) { + const worker = cluster.fork(); + worker.on('exit', (code, signal) => { + if (signal) { + console.log(`worker was killed by signal: ${signal}`); + } else if (code !== 0) { + console.log(`worker exited with error code: ${code}`); + } else { + console.log('worker success!'); + } + }); +} ``` ### Event: `'listening'` @@ -235,16 +239,12 @@ added: v0.7.0 Similar to the `cluster.on('listening')` event, but specific to this worker. ```mjs -import cluster from 'node:cluster'; - cluster.fork().on('listening', (address) => { // Worker is listening }); ``` ```cjs -const cluster = require('node:cluster'); - cluster.fork().on('listening', (address) => { // Worker is listening }); -- cgit v1.2.3