From aeae56fd84c7431cf10a2f0c8123da3ec38e9011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 21 Jan 2022 12:47:02 +0100 Subject: [PATCH] doc: modernize and simplify cluster example --- doc/api/cluster.md | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index d34b1bb795c363..767701fe990bdf 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -1073,29 +1073,17 @@ list happens before the last `'disconnect'` or `'exit'` event is emitted. ```mjs import cluster from 'cluster'; -// Go through all workers -function eachWorker(callback) { - for (const id in cluster.workers) { - callback(cluster.workers[id]); - } -} -eachWorker((worker) => { +for (const worker of Object.values(cluster.workers)) { worker.send('big announcement to all workers'); -}); +} ``` ```cjs const cluster = require('cluster'); -// Go through all workers -function eachWorker(callback) { - for (const id in cluster.workers) { - callback(cluster.workers[id]); - } -} -eachWorker((worker) => { +for (const worker of Object.values(cluster.workers)) { worker.send('big announcement to all workers'); -}); +} ``` Using the worker's unique id is the easiest way to locate the worker.