Bun.js has a useful native API to read periodical user input:
const prompt = "Type something: ";
process.stdout.write(prompt);
for await (const line of console) {
console.log(`You typed: ${line}`);
process.stdout.write(prompt);
}
But...
I want to read user inputs, outside a loop!
I found this solution:
const stdinIterator = process.stdin.iterator()
console.write('What is your first name?\n> ')
const firstName = (await stdinIterator.next()).value.toString().trimEnd()
console.log('Hello,', firstName)
console.write('What is your last name?\n> ')
const lastName = (await stdinIterator.next()).value.toString().trimEnd()
console.log('Welcome', `${firstName} ${lastName}`)
It works. But then the process is not terminated automatically (I need to press Ctrl+C manually).
prompt
API.const firstName = prompt('What is your first name?')
console.log('Hello,', firstName)
const lastName = prompt('What is your last name?')
console.log('Welcome', `${firstName} ${lastName}`)