-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
35 lines (32 loc) · 947 Bytes
/
example.js
File metadata and controls
35 lines (32 loc) · 947 Bytes
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
const throughConcurrent = require("through2-concurrent");
const fs = require("fs");
const { promisify } = require("util");
const split = require("split");
const writeFileAsync = promisify(fs.writeFile);
fs
.createReadStream("infiniteList.txt")
.pipe(split())
.pipe(throughConcurrent.obj({ maxConcurrency: 5 }, async function(domain, enc, done) {
try {
console.log("invoking callback");
const url = await sometimesAsyncFunction(domain);
if (url) {
this.push(url);
}
} catch (err) {
console.error(err);
}
done();
}));
let calledBefore = false;
async function sometimesAsyncFunction(domain) {
if (calledBefore) {
return "";
}
calledBefore = true;
// This is a readFile in my code, but clearly I don't want to have to stub that out
await writeFileAsync("123.txt", domain);
console.log("async operation finished");
const someUrl = "http://123.com";
return someUrl;
}