使用readline,如何一次读取一行?

时间:2015-10-21 01:39:35

标签: node.js

以下是代码:

{}

我期待输出为:

import fs from 'fs';
import readline from 'readline';

let rl;

rl = readline.createInterface({
    input: fs.createReadStream(__dirname + '/allCountries.txt')
});

rl.on('line', (line) => {
    rl.pause();

    console.log('pause');

    // Do something that takes time with the line....
    setTimeout(() => {
        console.log('resume');

        rl.resume();
    }, 500);
});

但输出是:

pause
resume
pause
resume
pause
resume
pause
resume
pause
resume
pause
resume
pause
resume
pause
resume
pause
resume

如何强制readline模块读取一行,暂停并等待读取恢复?

0 个答案:

没有答案