forked from plaid/deprecated-async-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
callbacks.js
36 lines (31 loc) · 927 Bytes
/
callbacks.js
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
28
29
30
31
32
33
34
35
36
'use strict';
const fs = require('fs');
const path = require('path');
const S = require('sanctuary');
const main = () => {
const dir = process.argv[2];
fs.readFile(path.join(dir, 'index.txt'), {encoding: 'utf8'}, (err, data) => {
if (err != null) {
process.stderr.write(String(err) + '\n');
process.exit(1);
}
const filenames = S.lines(data);
const $results = [];
let filesRead = 0;
filenames.forEach((file, idx) => {
fs.readFile(path.join(dir, file), {encoding: 'utf8'}, (err, data) => {
if (err != null) {
process.stderr.write(String(err) + '\n');
process.exit(1);
}
$results[idx] = data;
filesRead += 1;
if (filesRead === filenames.length) {
process.stdout.write($results.join(''));
process.exit(0);
}
});
});
});
};
if (process.mainModule.filename === __filename) main();