无法使用Promise获得异步函数的结果

时间:2019-04-10 12:19:26

标签: javascript node.js asynchronous

我正在编写一个node.js程序以从控制台读取。因此,我使用了“ readline”模块,但是即使我调用promise来获取它的结果以便进行操作(两种方式),但仍然没有得到预期的结果:

  

以下代码是我要使用的模块:

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;
};

  

以下代码段是我调用模块时的代码

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');
}

在上面的代码中,注释行显示了使用Promise类的替代方法。 以下几行显示提示的结果:

C:\node\notes>node app.js add
Starting app.js
what is the value: The value is: undefined of the key undefined
undefined
undefined
您能告诉我,问题出在哪里以及如何解决该问题。

0 个答案:

没有答案