Skip to content

Instantly share code, notes, and snippets.

@torus
Created June 21, 2020 13:05
Show Gist options
  • Save torus/b9fc1fa66be0a93e1e9f6b0d05ab09d4 to your computer and use it in GitHub Desktop.
Save torus/b9fc1fa66be0a93e1e9f6b0d05ab09d4 to your computer and use it in GitHub Desktop.
JavaScript でジェネレータを使ってファイルを 1 行ずつ読み込んで処理する練習。
const readline = require('readline');
async function processLineByLine(strm, eat) {
const rl = readline.createInterface({
input: strm,
crlfDelay: Infinity
});
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
eat(false, line)
}
eat(true)
}
function eat(gen) {
gen.next()
return (done, line) => gen.next({done, line})
}
function* main() {
while (true) {
var {done, line} = yield
if (done)
break
else
console.log(">>>", line)
}
console.log("DONE!")
}
processLineByLine(process.stdin, eat(main()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment