I am basically trying to write a very basic program that will work like this:
Enter your name: _
Enter your age: _
Your name is <name> and your age is <age>.
I've been trying to figure out how to do something like this in Node without using the prompt
npm module.
My attempt at this was:
import readline from 'readline'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('What is your name? ', (name) => {
rl.question('What is your age? ', (age) => {
console.log(`Your name is ${name} and your age is ${age}`)
})
})
However, this nested way of doing it seems weird, is there anyway I can do it without making it nested like this to get the right order?
[count] : a statement telling someone that you will definitely do something or that something will definitely happen in the future. I'll be here early tomorrow, and that's a promise. [=I promise that I'll be here early tomorrow]
Promises are used to handle asynchronous operations in JavaScript. They are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code.
It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
A promise is an object that may produce a single value some time in the future : either a resolved value, or a reason that it's not resolved (e.g., a network error occurred). A promise may be in one of 3 possible states: fulfilled, rejected, or pending.
zangw's answer would be sufficient, but I think I can make it clearer:
import readline from 'readline'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
function askName() {
return new Promise((resolve) => {
rl.question('What is your name? ', (name) => { resolve(name) })
})
}
function askAge(name) {
return new Promise((resolve) => {
rl.question('What is your age? ', (age) => { resolve([name, age]) })
})
}
function outputEverything([name, age]) {
console.log(`Your name is ${name} and your age is ${age}`)
}
askName().then(askAge).then(outputEverything)
if you don't care about wether it ask both questions sequentially, you could do:
//the other two stay the same, but we don't need the name or the arrays now
function askAge() {
return new Promise((resolve) => {
rl.question('What is your age? ', (age) => { resolve(age) })
})
}
Promise.all([askName, askAge]).then(outputEverything)
Here is one example with Q
var readline = require('readline');
var Q = require('q');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var q1 = function () {
var defer = Q.defer();
rl.question('What is your name? ', (name) => {
defer.resolve(name);
});
return defer.promise;
};
q1().then(function(name) {
rl.question('What is your age? ', (age) => {
console.log(`Your name is ${name} and your age is ${age}`)
});
});
Or with simple Promise
function question1() {
return new Promise(function(resolve) {
rl.question('What is your name? ', (name) => {
resolve(name);
});
});
};
question1().then(function(name) {
rl.question('What is your age? ', (age) => {
console.log(`Your name is ${name} and your age is ${age}`)
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With