forked from plaid/deprecated-async-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coroutines-bluebird.js
43 lines (34 loc) · 1.15 KB
/
coroutines-bluebird.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
'use strict';
const fs = require('fs');
const path = require('path');
const bluebird = require('bluebird');
const R = require('ramda');
const S = require('sanctuary');
// readFile :: String -> String -> Promise Error String
const readFile = R.curry((encoding, filename) =>
bluebird.promisify(fs.readFile)(filename, {encoding: encoding})
);
// readFiles :: String -> [String] -> Promise Error [String]
const readFiles = R.curry((encoding, filenames) =>
bluebird.all(R.map(readFile(encoding), filenames))
);
// walk :: String -> Promise Error String
const walk = bluebird.coroutine(function*(dir) {
const pathTo = (filename) => path.join(dir, filename);
const index = yield readFile('utf8', pathTo('index.txt'));
const filenames = S.lines(index).map(pathTo);
const results = yield readFiles('utf8', filenames);
return results.join('');
});
const main = () => {
walk(process.argv[2])
.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();