forked from plaid/deprecated-async-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
promises.js
40 lines (33 loc) · 933 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
36
37
38
39
40
'use strict';
const fs = require('fs');
const path = require('path');
const S = require('sanctuary');
// readFile :: (Object, String) -> Promise Error 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(S.lines)
.then(filenames => Promise.all(
filenames.map(file => readFile({encoding: 'utf8'}, path.join(dir, file)))
))
.then(results => results.join(''))
.then(data => {
process.stdout.write(data);
process.exit(0);
},
err => {
process.stderr.write(String(err) + '\n');
process.exit(1);
});
};
if (process.mainModule.filename === __filename) main();