I'm trying to use the node.js package readline to get user input on the command line, and I want to pipe the entered input through promises. However, the input never gets through the then chain. I think the problem could come from the fact that the promises are fulfilled in the callback method, but I don't know how to solve that problem.
An example of this problem looks like this:
import rlp = require('readline');
const rl = rlp.createInterface({
input: process.stdin,
output: process.stdout
});
let prom = new Promise(resolve => {
rl.question('Enter input: ', input => rl.close() && resolve(input));
});
prom
.then(result => { console.log(result); return prom; })
.then(result => { console.log(result); return prom; })
.then(result => console.log(result));
If run in node.js, the question will appear once, after input has been entered the program just stops. I want it to wait until the first input has been entered, then it should print this input and ask for the next input.
Thanks in advance!
Once your promise is resolved, there's no use of waiting for that again. I also moved the rl.close() call to the end, as it's needed to be called only once.
const rlp = require('readline');
const rl = rlp.createInterface({
input: process.stdin,
output: process.stdout
});
function ask() {
return new Promise(resolve => {
rl.question('Enter input: ', input => resolve(input));
});
}
ask()
.then(result => { console.log(result); return ask(); })
.then(result => { console.log(result); return ask(); })
.then(result => { console.log(result); rl.close() });
Here's an answer from this question here for which I deserve no credit.
// Function
function Ask(query) {
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise(resolve => readline.question(query, ans => {
readline.close();
resolve(ans);
}))
}
// example useage
async function main() {
var name = await Ask("whats you name")
console.log(`nice to meet you ${name}`)
var age = await Ask("How old are you?")
console.log(`Wow what a fantastic age, imagine just being ${age}`)
}
main()
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const ask = (query) => new Promise((resolve) => rl.question(query, resolve));
ask('A: ').then(async (a) => {
const b = await ask('B: ');
const c = await ask('B: ');
console.log(a, b, c);
rl.close();
});
rl.on('close', () => process.exit(0));
Node.js 17 is here with new promise-based APIs for readline module:
import * as readline from 'node:readline/promises'
import { stdin as input, stdout as output } from 'process'
const rl = readline.createInterface({input, output})
const answer = await rl.question('What do you think of Node.js? ')
console.log(`Thank you for your valuable feedback: ${answer}`)
rl.close()
https://nodejs.org/api/readline.html#readline
node prompt.mjs
import { createInterface as createQuestionInterface } from 'readline';
const rl = createQuestionInterface({
input: process.stdin,
output: process.stdout
});
function questionLine(multiline, resolve, i, input, rl) {
if (!multiline) {
resolve(i);
} else {
if (input && !i) {
resolve(input);
} else {
return input + i + "\r\n";
}
}
return input;
}
function promptMultiLine(questionText) { // This is async by returning promise
return prompt(questionText, true);
}
async function prompt(questionText, multiline = false) {
return await (new Promise((resolve, reject) => {
let input = '';
rl.question(`${questionText}: `, (i) => {
input = questionLine(multiline, resolve, i, input, rl);
});
rl.on('line', (i) => {
input = questionLine(multiline, resolve, i, input, rl);
});
}));
}
async function run() {
const question = prompt("please enter response [enter to complete]");
console.log(question);
const questionMultiLine = promptMultiLine("please enter response [enter text and enter twice]");
console.log(questionMultiLine);
}
run();
Related
How can I put data from an external txt file to function argument? I get undefined because reading files is asynchronous and the values which I give to the functions are undefined. How can I make it work? Each line is one variable.
let numberOfElephants;
let massOfElephants;
let elephantsOrderGiven;
let proposedOrder;
const readFile = () => {
return new Promise((resolve, reject) => {
const lineArray = [];
const readInterface = readline.createInterface({
input: fs.createReadStream("./slo1.in"),
output: process.stdout,
terminal: false,
});
readInterface
.on("line", (line) => {
lineArray.push(line);
})
.on("close", () => {
resolve(lineArray);
});
});
};
async function readAsync() {
const res = await readFile();
numberOfElephants= res[0],
massOfElephants= res[1],
elephantsOrderGiven= res[2],
proposedOrder= res[3],
}
console.log("read");
readAsync();
const sortElephants = (
numberOfElephants,
massOfElephants,
elephantsOrder,
directorsOrder
) => {}
sortElephants(
numberOfElephants,
massOfElephants,
elephantsOrderGiven,
proposedOrder
)
I need a node js function that goes through two arrays and waits for user input every time and then continues to loop.
let array = **SOME_ARRAY**;
let array_2 = **SOME_ARRAY**;
for (let i in array) {
for (let j in array_2) {
process.stdin.on('data', data => {
data = data.toString();
data = data.replaceAll('\r', '').replaceAll('\n', '');
console.log(data, array[i], array_2[j]);
});
}
}
You could use readline.question with async-await:
const readline = require('readline');
function readInput() {
const interface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => interface.question("Please provide next input: ", answer => {
interface.close();
resolve(answer);
}))
}
(async () => {
const array = [1, 2];
const array2 = [4, 5];
for (const a of array) {
for (const b of array2) {
const data = await readInput();
// Do something with your data...
console.log(data, a, b);
}
}
})();
How to correct work with process.stdin in node worker_threads ...
I want to enter some value (code), but is stoped on input .. ?
tried 2 methods, but both same, stopped on input.
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
(async () => {
if (isMainThread) {
const worker = new Worker(__filename, {
stdin: true,
// stdout: true,
workerData: { text: 'Enter Code: ' }
});
} else {
console.log(workerData.text);
// Method-1
// answer = await new Promise(resolve => {
// process.stdin.once('data', (chunk) => {
// const code = chunk.toString().trim();
// console.log(`Captcha Code : ${code}`);
// resolve(code);
// });
// });
// Method-2
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
answer = await new Promise(resolve => {
rl.question('Code: ', (answer) => {
console.log(`Answer: ${answer}`);
rl.close();
resolve(answer);
});
});
}
})();
I am attempting to get both username and password from the CLI in a puppeteer project. I get it to ask one question and can use the value just fine, but when I do the second it just freezes on the input. It is almost like it is not actually closing and returning. I cannot seem to figure out what I am missing. I tried to declare the interface in the question method and then destroy it when close is called, but that did not work. I feel like I am close, but I cannot figure out what I am missing.
const login = require('../common/login.js');
userId = await login.getUserId();
console.log(userId) //works
password = await login.getPassword();
console.log(password) //does not work
login.js
const readline = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
var methods = {};
const question = (promptText) => {
let response;
readline.setPrompt(promptText)
readline.prompt();
return new Promise((resolve, reject) => {
readline.on('line', (userInput) => {
console.log('hi');
response = userInput;
readline.close();
});
readline.on('close', () => {
console.log('bye');
resolve(response);
})
})
};
methods.getUserId = async() => {
let username = question("Username: ");
return username;
}
methods.getPassword = async() => {
let password = question("Password: ");
console.log(password);
return password;
}
module.exports = methods;
The readline.close() will close the streams. The below code works for me
const rli = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
function input(prompt) {
return new Promise((callbackFn, errorFn) => {
rli.question(prompt, (uinput)=> {
callbackFn(uinput);
}, ()=> {
errorFn();
});
});
}
const main = async () => {
amt = await input("Enter the amount to check: ");
doSomething(amt);
uname = await input("Enter your name: ");
doSomethingElse(uname);
rli.close();
};
main();
I wrote an async function in nodejs that return value of a query in my database. I tested this query and it worked. but my problem is in the define of readline. when i run this code I get an error that :
const a = await Movie.find({}).sort('-year').where('year').gt(X).lt(Y).sort('rank')
^^^^^
SyntaxError: await is only valid in async function
how should I define readline function as async function?
this is my function :
async function returnMoviesBetweenXandY(){
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('enter the first number : ', (X) => {
rl.question('enter the second number : ', (Y) => {
const a = await Movie.find({}).sort('-year').where('year').gt(X).lt(Y).sort('rank')
const temp =await Promise.map(a, getTitle)
return temp
// rl.close();
});
});
}
returnMoviesBetweenXandY().then(function(result){console.log(result)})
Use async keyword before (X) => and (Y) => functions as lambda representation. Such as:
rl.question('enter the first number : ', async (X) => {
rl.question('enter the second number : ', async (Y) => {
const a = await Movie.find({}).sort('-year').where('year').gt(X).lt(Y).sort('rank')
const temp =await Promise.map(a, getTitle)
return temp
// rl.close();
});
});
Basically, the function should be marked async if you are using await keyword in the body of the function. In your case, you are using await keywords inside the lambda functions. So it should be marked as async.