-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathread.js
More file actions
27 lines (24 loc) · 673 Bytes
/
read.js
File metadata and controls
27 lines (24 loc) · 673 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { createReadStream } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { pipeline } from 'node:stream';
const read = async () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const filePath = path.join(__dirname, 'files', 'fileToRead.txt');
await new Promise((resolve, reject) => {
pipeline(createReadStream(filePath), process.stdout, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
};
try {
await read();
} catch (e) {
console.error('Stream read error:', e.message);
process.exitCode = 1;
}