5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-04-16

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();
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?