JavaScript
Node.js

Node.jsでのパイプ使用(readline)

Node.jsでの標準入力(コンソール)についての続編っぽいようなそうでもないような記事。

前述していた記事を書いていた時にパイプの動作について勘違いしていたことを見つけたので動作を試してみた次第。
結論としてはPromiseを挟むとパイプでは正常に動作ができないようだった。

なので、パイプする場合は素直にprocess.stdin.onを使うかasync/awaitをあきらめる必要があるという何とも残念な結論。

あとrl.questionはパイプ非対応だった気がしたけど、使えたんだね。

以下実験で使用したソースファイルもろもろ。

実験用ソース

AsyncPromise

const readline=require("readline");
const rl= readline.createInterface({
    input:process.stdin,
    output:process.stdout,
    terminal:false
});

(async function(){
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
    console.log(await new Promise(res=>rl.once("line",s=>res(s))));
})();

GeneratorPromise

const readline=require("readline");
const rl= readline.createInterface({
    input:process.stdin,
    output:process.stdout,
    terminal:false
});

const g=(function*(){
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
    console.log(yield (new Promise(res=>rl.once("line",s=>res(s)))).then(s=>g.next(s)));
})();
g.next();

Generator

const readline=require("readline");
const rl= readline.createInterface({
    input:process.stdin,
    output:process.stdout,
    terminal:false
});

const g=(function*(){
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
    console.log(yield rl.once("line",s=>g.next(s)));
})();
g.next();

GeneratorQuestion

const readline=require("readline");
const rl= readline.createInterface({
    input:process.stdin,
    output:process.stdout,
    terminal:false
});

const g=(function*(){
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
    console.log(yield rl.question("",s=>g.next(s)));
})();
g.next();