Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点奇怪的用户输入异步行为_Javascript_Node.js_Promise_Command Line Interface - Fatal编程技术网

Javascript 节点奇怪的用户输入异步行为

Javascript 节点奇怪的用户输入异步行为,javascript,node.js,promise,command-line-interface,Javascript,Node.js,Promise,Command Line Interface,我刚刚习惯了节点编程,但我遇到了这个执行问题,对此我有点困惑 我试图测试一个写路径是否已经存在,如果已经存在,然后请求用户输入 function testPath(fileName) { fs.exists(path.resolve('.', fileName), function(exists) { //the filepath already exists, ask for user confirmation if(exists) { process.stdin.on('

我刚刚习惯了节点编程,但我遇到了这个执行问题,对此我有点困惑

我试图测试一个写路径是否已经存在,如果已经存在,然后请求用户输入

function testPath(fileName) {
  fs.exists(path.resolve('.', fileName), function(exists) {
  //the filepath already exists, ask for user confirmation
  if(exists) {
    process.stdin.on('keypress', function (str, key) {
    //print result of keypress to console
    console.log("str: ", str, " key: ", key);

    if ((str.toLowerCase() == "n") || (~["y", "n"].indexOf(str.toLowerCase()))) {
      return false;
    }
    else {
      return true;
    }
  });
  }
  else {
  //the filepath does not already exist - return true
  return true;
}
console.log("Filename in the target directory already exists, would you like to overwrite? (y/n)");
});
}
这个函数作为一个整体将通过对它的承诺来解决(或不解决)


“发送给用户”和“等待按键”的消息似乎以正确的方式运行,但它会卡在一个循环中,即使在有效的按键下也不会返回,有人知道这是为什么吗?

如果您想将其用作承诺,您需要返回一个承诺:

function testPath(fileName) {
    return new Promise((resolve, reject) => {
        fs.exists(path.resolve('.', fileName), function(exists) {
        //the filepath already exists, ask for user confirmation
        if(exists) {
            process.stdin.on('keypress', function (str, key) {
            //print result of keypress to console
            console.log("str: ", str, " key: ", key);

            if ((str.toLowerCase() == "n") || (~["y", "n"].indexOf(str.toLowerCase()))) {
            return reject();
            }
            else {
            return resolve();
            }
        });
        }
        else {
        //the filepath does not already exist - return true
        return resolve();
        }
        console.log("Filename in the target directory already exists, would you like to overwrite? (y/n)");
        });
        }
    })

您的
return true
return false
在回调中。它们不会从
testPath()
返回值。我真的不明白你的问题。也许这是一个重复?这句话:“这个功能作为一个整体将被解决(或不)的承诺呼吁它。”是完全不清楚的。您的代码显示承诺在任何地方都没有用处。另外,承诺并没有被“调用”。我将使用var=Promise.resolve(testPath()),现在这很明显是的一个副本。我会把它作为复制品关闭,但不能,因为我因为不清楚而投票关闭了它。其他人可以为此关闭它。您使用的
Promise.resolve(testPath())
是完全错误的。要使其工作,
testPath()
必须返回承诺本身,或者必须同步返回实际值—两者都不返回。Promise没有神奇的能力知道异步操作何时完成。你必须告诉他们何时完成。是的,我相信这就是OP所要求的(或者至少应该这样做)-如何“承诺”测试-即如何返回承诺,最终解决其成功路径或错误路径,以响应异步操作
fs.exists()
和导致
process.stdin.on('keypress')
启动的手动操作。不过,我认为他知道的还不够,不知道他在寻找什么。这就是我粘贴代码的原因。这样,他至少知道寻找允诺书,本质上是这样的。IMHO,你发布这个答案是正确的。