Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use promises for this example?

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?

like image 237
Saad Avatar asked Jan 16 '16 00:01

Saad


People also ask

What is promise give an example?

[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]

Where can I use promises?

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.

For what purpose promise is used?

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.

What is a promise Where and how would you use promise?

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.


2 Answers

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)
like image 140
fixmycode Avatar answered Sep 19 '22 15:09

fixmycode


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}`)
    });    
});
like image 35
zangw Avatar answered Sep 22 '22 15:09

zangw