Skip to content

Commit 7ac9ba0

Browse files
committed
feat: concurrent is better than parallel :)
1 parent f9ce38f commit 7ac9ba0

File tree

19 files changed

+34
-21
lines changed

19 files changed

+34
-21
lines changed

06-coding-with-streams/18-sequential-execution/concat-files.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ export function concatFiles(dest, files) {
1111
transform(filename, _enc, done) {
1212
const src = createReadStream(filename)
1313
src.pipe(destStream, { end: false })
14+
// same as ((err) => done(err))
15+
// propagates the error
1416
src.on('error', done)
17+
// same as (() => done())
18+
// propagates correct completion
1519
src.on('end', done)
1620
},
1721
})

06-coding-with-streams/19-unordered-parallel-execution/README.md renamed to 06-coding-with-streams/19-unordered-concurrent-execution/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# 19-unordered-parallel-execution
1+
# 19-unordered-concurrent-execution
22

3-
This example shows how to create a parallel execution flow using streams.
3+
This example shows how to create a concurrent execution flow using streams.
44

55
## Run
66

06-coding-with-streams/19-unordered-parallel-execution/check-urls.js renamed to 06-coding-with-streams/19-unordered-concurrent-execution/check-urls.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { createReadStream, createWriteStream } from 'node:fs'
22
import { createInterface } from 'node:readline'
33
import { pipeline } from 'node:stream/promises'
4-
import { ParallelStream } from './parallel-stream.js'
4+
import { ConcurrentStream } from './concurrent-stream.js'
55

66
const inputFile = createReadStream(process.argv[2])
77
const fileLines = createInterface({
88
input: inputFile,
99
})
10-
const checkUrls = new ParallelStream(async (url, _enc, push, done) => {
10+
const checkUrls = new ConcurrentStream(async (url, _enc, push, done) => {
1111
if (!url) {
1212
return done()
1313
}

06-coding-with-streams/19-unordered-parallel-execution/parallel-stream.js renamed to 06-coding-with-streams/19-unordered-concurrent-execution/concurrent-stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Transform } from 'node:stream'
22

3-
export class ParallelStream extends Transform {
3+
export class ConcurrentStream extends Transform {
44
constructor(userTransform, opts) {
55
super({ objectMode: true, ...opts })
66
this.userTransform = userTransform

06-coding-with-streams/19-unordered-parallel-execution/package.json renamed to 06-coding-with-streams/19-unordered-concurrent-execution/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "19-unordered-parallel-execution",
2+
"name": "19-unordered-concurrent-execution",
33
"version": "1.0.0",
4-
"description": "This example shows how to create a parallel execution flow using streams",
4+
"description": "This example shows how to create a concurrent execution flow using streams",
55
"type": "module",
66
"scripts": {},
77
"engines": {

06-coding-with-streams/20-unordered-limited-parallel-execution/README.md renamed to 06-coding-with-streams/20-unordered-limited-concurrent-execution/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# 20-unordered-limited-parallel-execution
1+
# 20-unordered-limited-concurrent-execution
22

3-
This example shows how to create a limited parallel execution flow using
3+
This example shows how to create a limited concurrent execution flow using
44
streams.
55

66
## Run

06-coding-with-streams/20-unordered-limited-parallel-execution/check-urls.js renamed to 06-coding-with-streams/20-unordered-limited-concurrent-execution/check-urls.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { createReadStream, createWriteStream } from 'node:fs'
22
import { createInterface } from 'node:readline'
33
import { pipeline } from 'node:stream/promises'
4-
import { LimitedParallelStream } from './limited-parallel-stream.js'
4+
import { LimitedConcurrentStream } from './limited-concurrent-stream.js'
55

66
const inputFile = createReadStream(process.argv[2])
77
const fileLines = createInterface({
88
input: inputFile,
99
})
10-
const checkUrls = new LimitedParallelStream(
10+
const checkUrls = new LimitedConcurrentStream(
1111
8,
1212
async (url, _enc, push, done) => {
1313
if (!url) {
1414
return done()
1515
}
1616
try {
17-
await fetch(url, { method: 'HEAD', timeout: 5 * 1000 })
17+
await fetch(url, {
18+
method: 'HEAD',
19+
timeout: 5000,
20+
signal: AbortSignal.timeout(5000),
21+
})
1822
push(`${url} is up\n`)
1923
} catch (err) {
2024
push(`${url} is down: ${err}\n`)

0 commit comments

Comments
 (0)