0

I put together a script for grabbing user input via readline. The simplified version of the logic that's failing is as follows:

const rl = require('readline').createInterface({
        input: process.stdin,
        output: process.stdout
});

function ask() {
    return new Promise((resolve, reject) => {
        rl.question('Enter input:', (input) => resolve(input) );
    });
}

async function logic() {
    let result = await ask();
    console.log(result); // this console.log seems fine
    rl.close()
    return result;
}

let a = logic();
console.log(a); // this console.log causes the issue

When I run the above, the output is:

Enter input: Promise { <pending> }

The culprit seems to be console.log() following the call to logic(). If I remove it, data is requested as expected and operation continues. But getting rid of all log messages in the code after readline won't work for this script. What can I do to fix this? Do I need to do something to process.stdout after closing input?

Alexander Tsepkov
  • 3,946
  • 3
  • 35
  • 59
  • Possible 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) – Liam Jun 04 '19 at 13:50
  • @Liam I checked the question, I don't believe they're at all related. If you still think otherwise, please explain how the answer to that question would solve this problem. As I've been playing with the code more, it seems to be caused by console.log() following the async function call. But even if I remove that, there are more log messages. – Alexander Tsepkov Jun 04 '19 at 14:03
  • if it's printing `Promise { }` thats the string result of an unresolved promise, i.e. a asynchounous block of code that has not yet completed, i.e. your trying to return the response from an async block of code, which you cannot directly do. It's totally unclear why `ask` creates a promise. None of that code is async and all you do is return it, so it's not doing anything. If that doesn't answer your question then you need to create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) as it's very unclear where the promise resides – Liam Jun 04 '19 at 14:10
  • The console.log simply ads a delay into your code allowing the promise to resolve. It's a red herring, ignore it. My guess is you simply need to remove the promise from `ask` – Liam Jun 04 '19 at 14:11
  • I'd also recommend you read [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) as I think this is the problem your trying to resolve with that rogue promise – Liam Jun 04 '19 at 14:13
  • @Liam, readline returns the response as a callback. The promise is there to convert the callback into a format I can `await` from the main logic body. As far as the console.log, it's not the presence of it that fixes the issue, but the absence, so it's not that it's giving the function enough time to resolve. – Alexander Tsepkov Jun 04 '19 at 14:20

0 Answers0