forked from plaid/deprecated-async-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promises.js
35 lines (29 loc) · 781 Bytes
/
promises.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
'use strict';
const fs = require('fs');
const path = require('path');
// readFile :: (Object, String) -> Promise String
const readFile = (options, filename) =>
new Promise((res, rej) => {
fs.readFile(filename, options, (err, data) => {
if (err != null) {
rej(err);
} else {
res(data);
}
});
});
const main = () => {
const dir = process.argv[2];
readFile({encoding: 'utf8'}, path.join(dir, 'index.txt'))
.then(data => Promise.all(
data
.match(/^.*(?=\n)/gm)
.map(file => readFile({encoding: 'utf8'}, path.join(dir, file)))
))
.then(data => process.stdout.write(data.join('')))
.catch(err => {
process.stderr.write(err.message);
process.exit(1);
});
};
if (process.argv[1] === __filename) main();