I'm writing a node.js program to read from console. Therefore ,I used "readline" module, but even If I called promise to get Its result in order to manipulate it (in both ways), but still I don't get the expected result:
The below code is the module I want to use:
module.exports.getContentFromPrompt=function (query){
var read= require('readline');
var input;
var rl=read.createInterface({
input:process.stdin,
output: process.stdout
});
rl.question(query,(answer)=>{
input=answer;
console.log(answer);
});
var promise = new Promise((resolve, reject)=>{
resolve(input);
});
promise.then((successMessage)=>{
console.log(successMessage);
})
rl.close();
return input;
};
The following snippet is the code when I call my module
console.log("Starting app.js");
const fs= require('fs');
const _=require('lodash');
//const prompt= require('prompt')
var readline= require('readline');
var process= require('process');
// const notes= require('./notes.js')
var inputFromPrompt = require('./read.js');
arg=process.argv;
var notes=new Map();
if(arg[2]=="list-all"){
notes.array.forEach((value, key) => {
console.log(`the value of the key: ${key} is ${value}`);
});
}
else if(arg[2]=="search"){
var elt2search=inputFromPrompt.getContentFromPrompt("what is the key of the element you're searching:");
searchedNote=notes.get(elt2search);
if(searchedNote==undefined){
console.log('the element with the key '+ elt2search+' does not exist');
}else{
console.log('the value of the element with the key '+elt2search+' is: '+searchedNote);
}
}
else if(arg[2]=="remove"){
var elt2delete=inputFromPrompt.getContentFromPrompt("what is the key of the element you want to delete:");
searchedNote=notes.delete(elt2delete);
if(searchedNote==undefined){
console.log('the element with the key '+ elt2delete+' does not exist');
}else{
console.log('the value of the element withe the key '+elt2delete+' is: '+elt2delete)
}
}
else if(arg[2]=="add"){
// var promise4Key = new Promise(function(resolve, reject){
// resolve(inputFromPrompt.getContentFromPrompt("what is the key of the element you want to add:"));
// })
// var promise4Value = new Promise((resolve, reject)=>{
// resolve(inputFromPrompt.getContentFromPrompt("what is the value of the element you want to add:"));
// })
var key, value;
// promise4Key.then((successMessage)=>{
// key=successMessage;
// })
// promise4Value.then((successMessage)=>{
// value=successMessage;
// });
key= inputFromPrompt.getContentFromPrompt("what is the key: ");
value= inputFromPrompt.getContentFromPrompt("what is the value: ");
notes=notes.set(key,value);
notes.forEach((value, key)=>{
console.log("The value is: "+ value+" of the key "+key);
});
}
else{
console.log('The command you typed: "'+ process.argv[2]+'" is not valid');
}
In the above code, the commented line shows the alternative to use the Promise class. The following lines shows the result that is prompted:
C:\node\notes>node app.js add
Starting app.js
what is the value: The value is: undefined of the key undefined
undefined
undefined