rl[Symbol.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() 一旦被调用就会开始消耗输入流。在创建接口和异步迭代之间进行异步操作可能会导致遗漏行。