Tuesday, 2 October 2018

node.js: readline: rl.question(query, callback)

rl.question(query, callback)
This method displays the query by writing it to the output, waits for user input to be provided on input, then invokes the callback function passing the provided input as the first argument.

Below table summarizes the arguments of question method.
Argument
Type
Description
query
string
A statement to write to output.
callback
Function
A callback function that is invoked with the user's input in response to the query.

Example
rl.question("What is your name ?", (name) => {
         console.log(`Hello ${name}, how are you`);
         rl.close();
});

Find the below working application.

HelloWorld.js
/* Import readline module */
var readline = require('readline');

/*
 * create a new readline.Interface instance that reads data from standard input
 * and write data to standard output
 */
var rl = readline.createInterface({
 input : process.stdin,
 output : process.stdout
});

rl.question("What is your name ?", (name) => {
 console.log(`Hello ${name}, how are you`);
 rl.close();
});

Output
What is your name ?Krishna
Hello Krishna, how are you



Previous                                                 Next                                                 Home

No comments:

Post a Comment