-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecondExample_without_await.js
42 lines (36 loc) · 1.13 KB
/
secondExample_without_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
const fs = require('fs');
const aw = require('awaitify-stream');
const lineLength = 6; // 5 digits in a zip code, plus the newline character.
function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
function run() {
let stream = aw.addAsyncFunctions(fs.createReadStream('zipCodes.lftxt'));
stream.setEncoding('utf8');
return new Promise((resolve, reject) => {
function readNextLine() {
stream.readAsync(lineLength).then((zipCode) => {
if (zipCode !== null) {
// Remove the newline character. If you didn't set the encoding
// above, use zipCode.toString().trim()
zipCode = zipCode.trim();
console.log(zipCode);
delay(200)
.then(readNextLine);
}
else {
// We've hit the end of the file. We're done.
resolve();
}
})
.catch(reject);
}
readNextLine();
});
}
run()
.then(() => {
console.log('All done!');
});