问题描述
我正在尝试通过自己正在使用的stdin接收数据:
I am trying to receive data via stdin which I have working:
const fs = require('fs');
const readStream = process.stdin;
readStream.pause();
readStream.setEncoding('utf8');
readStream.on('data', (data) => {
console.log(data);
});
readStream.resume();
现在我需要做的是将其存储为变量,这样我就可以在通过stdout返回它之前进行一些计算.
Now what I need to do is store it as a variable so I can do some calculations before I return it via stdout.
每次我尝试使用它做任何事情时,例如将某些数据推送到数组中,它都会重复执行数据,直到stdin完成为止,而在完成之后我将无法访问它.我找不到任何在线资源来帮助我.
Every time I try to so anything with it, like push certain data to an array it repeats the data until the stdin has finished, and I cant access it after it has finished. I cant find any resources online to help me.
推荐答案
您可以使用ReadableStream
处理异步任务.调用ReadableStream
实例的.getReader()
以获取具有.read()
方法的对象,该方法在被调用时返回具有value
和done
属性的对象.传递给构造函数的控制器可以将要执行的任务排队,对read()
的调用读取排队的数据,并将数据设置为value
属性.
You can use ReadableStream
to process asynchronous tasks. Call .getReader()
of ReadableStream
instance to get an object which has a .read()
method which when called returns an object having value
and done
properties. The controller passed to the constructor can queue tasks to be performed, the call to read()
reads the enqueued data and sets the data at value
property.
let n = 0;
let letters = "abcdefghijklmnopqrstuvwxyz";
const results = [];
let readableStream = new ReadableStream({
pull(controller) {
if (n < letters.length) controller.enqueue(letters[n++])
else controller.close();
}
});
let reader = readableStream.getReader();
let processStream = ({value, done}) => {
if (done) return reader.closed.then(() => results);
console.log(value);
// do stuff
let next = new Promise(resolve =>
setTimeout(() => {results.push(value); resolve()}
, Math.floor(Math.random() * 1200))
);
return next.then(() => reader.read())
.then(data => processStream(data));
}
reader.read()
.then(data => processStream(data))
.then(res => console.log(res));
您还可以通过管道实例的ReadableStream
和.pipeTo()
方法将排队的数据管道到WritableStream
实例,在该实例中,可以在流的读写部分执行任务.
You can also pipe the ReadableStream
and .pipeTo()
method of the instance to pipe the enqueued data to a WritableStream
instance, where tasks can be performed at both read and write portions of the stream.
const letters = "abcdefghijklmnopqrstuvwxyz";
let RSController = class {
constructor(input) {
this.input = input;
this.n = 0;
}
async pull(controller) {
if (this.n < this.input.length) {
let curr = await this.handleData(this.input[this.n]);
++this.n;
controller.enqueue(curr);
} else {
this.n = 0;
controller.close();
}
}
cancel(err) {
console.log(err)
}
handleData(data) {
return new Promise(resolve => {
// do stuff
setTimeout(() => {
resolve(data)
}, Math.floor(Math.random() * 750))
})
}
}
let WSController = class {
constructor(arr = []) {
this.results = arr;
}
write(data) {
let dataUp = data.toUpperCase();
console.log(data, dataUp);
this.results.push([data, dataUp]);
}
close() {
console.log(JSON.stringify(this.results, null, 2));
}
abort(e) {
console.error(e);
}
}
let rscontroller = new RSController(letters);
let readableStream = new ReadableStream(rscontroller);
let wscontroller = new WSController([]);
let writableStream = new WritableStream(wscontroller);
readableStream
.pipeTo(writableStream);
这篇关于通过stdin接收数据并存储为变量/数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!