1

I am just getting used to Node programming, but I have come across this execution issue which I am slightly confused about.

I am trying to test if a write path already exists and if it does then ask the user for input.

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

This function as a whole will be be resolved (or not) by a promise called on it.

The message to user and wait for keypress seem to action in the correct way but it sticks in a loop and never returns even on a valid keypress, does anyone know why this would be?

Scott Anderson
  • 631
  • 5
  • 26
  • 1
    Your `return true` and `return false` are INSIDE the callback. They do not return values from `testPath()`. I can't really understand your question. Perhaps it's a duplicate of [How do I return the value from an asynchronous function](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323)? – jfriend00 Dec 16 '18 at 20:28
  • This statement: **"This function as a whole will be be resolved (or not) by a promise called on it."** is completely unclear. Your code shows no use of promises anywhere. Also, promises aren't "called". – jfriend00 Dec 16 '18 at 20:29
  • I will use var = Promise.resolve(testPath()) – Scott Anderson Dec 16 '18 at 20:33
  • Now this is most clearly a duplicate of [How do I return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323). I would close it as a duplicate, but can't because I voted to close it for being unclear. Someone else can close it for that. – jfriend00 Dec 16 '18 at 20:39
  • Your use of `Promise.resolve(testPath()) ` is completely wrong. For that to work, `testPath()` would have to return a promise itself or would have to synchronously return the actual value - it does neither. Promise have no magic ability to know when asynchronous operations are done. You have to tell them when it's done. – jfriend00 Dec 16 '18 at 20:40
  • That's not the impression I got from this post: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then -- look for the but where it says 'const resolvedProm' – Scott Anderson Dec 16 '18 at 20:42
  • Well, that example does `Promise.resolve(33)`. That's a synchronous value. Nothing like what you're trying to do. You will need to do a bunch more reading about how promises work and how to use them (beyond what we can teach in a comment here). Please read the other answer I've linked in my previous comments. It teaches a LOT. – jfriend00 Dec 16 '18 at 20:45

1 Answers1

2

If you want to use it as a promise, you need to return a promise:

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)");
        });
        }
    })
Danilo Souza Morães
  • 1,481
  • 13
  • 18
  • Yes, I believe this is what the OP was asking for (or at least should have done so) - how to "promisify" the test - ie how to return a promise that will eventually settle to its success path or its error path in response to the asynchronous operation `fs.exists()` *and* the manual operation that causes `process.stdin.on('keypress')` to fire. – Roamer-1888 Dec 16 '18 at 21:50
  • I think he doesn't know enough to know that's what he is looking for, though. This is why I just pasted the code. This way he at least knows to look for promises – Danilo Souza Morães Dec 17 '18 at 00:04
  • Yes, essentially so. IMHO, you have done the right thing by posting this answer. – Roamer-1888 Dec 17 '18 at 00:29