rl[Symbol.asyncIterator]()
-
¥Returns: <AsyncIterator>
创建 AsyncIterator
对象,该对象遍历输入流中的每一行作为字符串。此方法允许通过 for await...of
循环异步迭代 InterfaceConstructor
对象。
¥Create an AsyncIterator
object that iterates through each line in the input
stream as a string. This method allows asynchronous iteration of
InterfaceConstructor
objects through for await...of
loops.
输入流中的错误不会被转发。
¥Errors in the input stream are not forwarded.
如果循环以 break
、throw
或 return
终止,则将调用 rl.close()
。换句话说,迭代 InterfaceConstructor
将始终完全消费输入流。
¥If the loop is terminated with break
, throw
, or return
,
rl.close()
will be called. In other words, iterating over a
InterfaceConstructor
will always consume the input stream fully.
性能无法与传统的 'line'
事件 API 相提并论。对于性能敏感的应用,请改用 'line'
。
¥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()
将在调用后开始使用输入流。在接口创建和异步迭代之间进行异步操作可能会导致丢失行。
¥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.