readline是node内置库
源码 —以后记得看

function stepRead(callback) { let line = '' const input = process.stdin const output = process.stdout function onKeyPress(s) { output.write(s) line += s switch (s) { case '\r': //enter键 callback(line) input.pause() } } emitKeyPressEvents(input) input.on('keypress', onKeyPress) input.setRawMode(true) input.resume()}function emitKeyPressEvents(stream) { function onData(chunk) { g.next(chunk.toString()) } const g = emitKeys(stream) g.next() //传入data方法 stream.on('data', onData)}function* emitKeys(stream) { while (true) { let ch = yield stream.emit('keypress', ch) }}stepRead(function (e) { console.log('anser', e)})