-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: modernize code examples in the cluster.md #10270
Conversation
It also will be consistent with a previous code example.
@@ -30,17 +32,20 @@ if (cluster.isMaster) { | |||
res.writeHead(200); | |||
res.end('hello world\n'); | |||
}).listen(8000); | |||
|
|||
console.log(`Worker ${process.pid} is online`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use "started"? There is an online
event, and this isn't it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -807,9 +812,9 @@ before last `'disconnect'` or `'exit'` event is emitted. | |||
```js | |||
// Go through all workers | |||
function eachWorker(callback) { | |||
for (var id in cluster.workers) { | |||
Object.keys(cluster.workers).forEach((id) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is worse than original
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sam-github Should I also change the prototype into the for
...in
variant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sam-github Done.
`cluster.workers` iteration: `Object.keys().forEach` -> `for`...`in`
LGTM, should be squashed before landing. |
cluster.fork(); | ||
} | ||
|
||
Object.keys(cluster.workers).forEach((id) => { | ||
for (const id in cluster.workers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think it's better to keep Object.keys
here? for...in
also includes non own enumerable properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nvm for...in
is also used below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no such properties in cluster.workers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I know, but I think the intention is to also educate in examples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this example educates that there are no such properties, that cluster.workers
is a vanilla js object underived from anything, and doesn't require complex boiler plate safe-guards to be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough :)
ping @nodejs/documentation |
Landed in e03ee71 |
Checklist
Affected core subsystem(s)
doc, cluster
Description of change
var
->let
/const
==
->===
cluster.workers
iteration:Object.keys().forEach
->for
...in
Fixes: #10255