forked from slavaGanzin/async-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
await.js
45 lines (38 loc) · 1 KB
/
await.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
41
42
43
44
45
'use strict';
const fs = require('fs');
const path = require('path');
require('babel/polyfill');
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);
}
});
});
async function main() {
const index = path.join(process.argv[2], 'index.txt');
let data;
try {
data = await readFile({encoding: 'utf8'}, index);
} catch (err) {
process.stderr.write(String(err) + '\n');
process.exit(1);
}
const filenames = S.lines(data);
let results;
try {
results = await* filenames.map(filename =>
readFile({encoding: 'utf8'}, path.join('input', filename))
);
} catch (err) {
process.stderr.write(String(err) + '\n');
process.exit(1);
}
process.stdout.write(results.join(''));
};
if (process.mainModule.filename === __filename) main();