Fun4wut

JS的readline函数简易封装

Word count: 164 / Reading time: 1 min
2018/06/18 Share

JS一直没有一个很好的处理标准输入的方法,在以前,我们需要这样做

1
2
3
4
5
6
7
8
let readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.on('line', (line)=> {
line.split('<br>').forEach((val) => console.log(val));
})

我们只能用监听换行符来回调去实现标准输入,但这是非常不自然的,结合ES6/8的Promise和async,我们可以对其进行一个简易封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let linemod= require ('readline')
const rl = linemod.createInterface({
input: process.stdin,
output: process.stdout
});

const readline = ()=>{
return new Promise((resolve,reject)=>{
rl.on('line',(line)=>{
resolve(line)
})
})
}
const main = async ()=>{
let a = await readline()
//todo
}

CATALOG