Skip to content

Commit 4a335c4

Browse files
committed
Consistent comments and formatting
1 parent 462f926 commit 4a335c4

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

child-processes/fork-child.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const fibonacci = (num) => num <= 1 ? 1 : fibonacci(num - 1) + fibonacci(num - 2)
1+
const fibonacci = (num) => num <= 1 ? 1 : fibonacci(num - 1) + fibonacci(num - 2);
22

33
process.on('message', ({n}) => {
4-
process.send({ fib: fibonacci(n), n })
4+
process.send({ fib: fibonacci(n), n });
55
// optional - there is no reason why this child process
66
// can't be called multiple times.
7-
process.exit()
7+
process.exit();
88
})

child-processes/fork-parent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { fork } = require('child_process')
1+
const { fork } = require('child_process');
22

3-
const child1 = fork('fork-child')
4-
const child2 = fork('fork-child')
5-
const child3 = fork('fork-child')
3+
const child1 = fork('fork-child');
4+
const child2 = fork('fork-child');
5+
const child3 = fork('fork-child');
66

77
// send data to the child process to perform the calculation
88
child1.send({ n: 5 });

child-processes/index.md

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ url: child-processes
55
author: ian
66
---
77

8-
<<<<<<< HEAD
98
_Prerequisites: [events](../events), [streams](../buffers-and-streams)_
109

1110
The typical operating system has different processes running in the background, and each process is being managed by a single-core of our CPU and will run a series of calculations each time it is being ticked. To take full advantage of our CPU using a single process, we would need a number of processes that is at least equal to the number of cores in our CPU. In addition, each process might be responsible for running a series of calculations of different logic, which will give the end user a better control over the CPU’s behavior.
@@ -32,7 +31,8 @@ Typically, `spawn` is more suitable for long-running process with large outputs.
3231

3332
## A Simple `exec`
3433

35-
In this example, we will call the `ls` command to listwith the optional param `-l` to show a long list of details.
34+
In this example, we will call the `ls` command to listwith the optional param `-l` to show a long list of details.
35+
3636
<div class="repl-code">
3737

3838
```javascript
@@ -57,7 +57,9 @@ We will now implement the previous example using the `spawn` method:
5757
<div class="repl-code">
5858

5959
```javascript
60+
6061
const { spawn } = require('child_process');
62+
6163
const ls = spawn('ls', ['-l']);
6264

6365
ls.stdout.on('data', (data) => {
@@ -84,12 +86,14 @@ We are going to run two child processes, and pipe the output from one as the inp
8486
<div class="repl-code">
8587

8688
```javascript
89+
8790
const { spawn } = require('child_process');
91+
8892
const ls = spawn('ls', ['-l']);
89-
const wc = spawn('wc')
93+
const wc = spawn('wc');
9094

9195
// pipe output from ls as input to wc
92-
ls.stdout.pipe(wc.stdin)
96+
ls.stdout.pipe(wc.stdin);
9397

9498
wc.stdout.on('data', (data) => {
9599
console.log(`wc stdout: ${data}`);
@@ -116,25 +120,26 @@ In this example, we are going to create a child process that can receive a numbe
116120

117121
```javascript
118122

119-
const fibonacci = (num) => num <= 1 ? 1 : fibonacci(num - 1) + fibonacci(num - 2)
123+
const fibonacci = (num) => num <= 1 ? 1 : fibonacci(num - 1) + fibonacci(num - 2);
120124

121125
process.on('message', ({ n }) => {
122-
process.send({ fib: fibonacci(n), n })
126+
process.send({ fib: fibonacci(n), n });
123127
// optional - there is no reason why this child process
124128
// can't be called multiple times.
125-
process.exit()
129+
process.exit();
126130
})
127131

128132
```
129133

130134
The parent process creates 3 child processes, and passes a range of numbers to them for calculating.
131135

132136
```javascript
133-
const { fork } = require('child_process')
134137

135-
const child1 = fork('fork-child')
136-
const child2 = fork('fork-child')
137-
const child3 = fork('fork-child')
138+
const { fork } = require('child_process');
139+
140+
const child1 = fork('fork-child');
141+
const child2 = fork('fork-child');
142+
const child3 = fork('fork-child');
138143

139144
// send data to the child process to perform the calculation
140145
child1.send({ n: 5 });

child-processes/spawn-with-pipes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const { spawn } = require('child_process');
2+
23
const ls = spawn('ls', ['-l']);
3-
const wc = spawn('wc')
4+
const wc = spawn('wc');
45

56
// pipe output from ls as input to wc
6-
ls.stdout.pipe(wc.stdin)
7+
ls.stdout.pipe(wc.stdin);
78

89
wc.stdout.on('data', (data) => {
910
console.log(`wc stdout: ${data}`);

0 commit comments

Comments
 (0)