From f4fa409534f2e1d34be3f9d21179b9cc89bed423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Sun, 23 Jan 2022 13:14:11 +0100 Subject: [PATCH] doc: modernize and simplify cluster example PR-URL: https://github.com/nodejs/node/pull/41626 Reviewed-By: Mestery Reviewed-By: Rich Trott Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca --- 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 5dfd8116dc5de0..10af5fa20a1e03 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -1074,29 +1074,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.