javascript - intergrate function readline from node.js module into read_line function to get user input -
//normal nodejs module readline const readline = require('readline'); //i want integrate readline module function read_line can user input use read_line ,but mind wrong. //i know reason js function not blocked function,but not familiar nodejs,how can block subsequent code run? function read_line() { var input; const rl = readline.createinterface({ input: process.stdin, output: process.stdout }); rl.on('line', function (input) { this.input = input; rl.close(); }); return input; } //it run right now,return undefined var s = read_line(); //i want console user input console.log(s);
i want integrate readline module function read_line can user input use read_line ,i need help!
readline asynchronous function
rl.on('line', function (input) { this.input = input; rl.close(); });
the function wait 'line' event triggered
when call function read_line() run synchronous , return input;
you can use callback function, when 'line' event triggered callback function called value
const readline = require('readline'); function read_line(cb) { const rl = readline.createinterface({ input: process.stdin, output: process.stdout }); rl.on('line', function (input) { cb(input) rl.close(); }); } read_line(function(input){ console.log(input); });
Comments
Post a Comment