rl[Symbol.asyncIterator]()
- 返回: <AsyncIterator>
创建 AsyncIterator
对象,该对象遍历输入流中的每一行作为字符串。
此方法允许通过 for await...of
循环异步迭代 readline.Interface
对象。
输入流中的错误不会被转发。
如果循环以 break
、throw
或 return
终止,则将调用 rl.close()
。
换句话说,迭代 readline.Interface
将始终完全消费输入流。
性能无法与传统的 'line'
事件 API 相提并论。
对于性能敏感的应用程序,请改用 'line'
。
async function processLineByLine() {
const rl = readline.createInterface({
// ...
});
for await (const line of rl) {
// 逐行读取输入中的每一行
// 都将在此处作为 `line` 连续可用。
}
}
readline.createInterface()
将在调用后开始使用输入流。
在接口创建和异步迭代之间进行异步操作可能会导致丢失行。
- Returns: <AsyncIterator>
Create an AsyncIterator
object that iterates through each line in the input
stream as a string. This method allows asynchronous iteration of
readline.Interface
objects through for await...of
loops.
Errors in the input stream are not forwarded.
If the loop is terminated with break
, throw
, or return
,
rl.close()
will be called. In other words, iterating over a
readline.Interface
will always consume the input stream fully.
Performance is not on par with the traditional 'line'
event API. Use 'line'
instead for performance-sensitive applications.
async function processLineByLine() {
const rl = readline.createInterface({
// ...
});
for await (const line of rl) {
// Each line in the readline input will be successively available here as
// `line`.
}
}
readline.createInterface()
will start to consume the input stream once
invoked. Having asynchronous operations between interface creation and
asynchronous iteration may result in missed lines.