0

Thats my code:

async function startPro(){
    await sendIt();
    const genKey = 1234
    
    const readline = require('readline').createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    readline.question('Please enter your key?', discKey => {
        console.log(`Your key: ${discKey}!`);
    });

    console.log(discKey.length);
    if (discKey.length == 4) {
        for (var x = 1; x <= taskNr; x++){
            openBrowser();
        }
    }
    else{
        console.log('nope')
    }
}
startPro();

This is the part that should make an input-field, but it kind of just gets skipped, which results in an error.

const readline = require('readline').createInterface({
        input: process.stdin,
        output: process.stdout
    });
    
    readline.question('Please enter your key?', discKey => {
        console.log(`Your key: ${discKey}!`);
    });

Does somebody know how to fix that? I tried a lot of things, but nothing worked.

  • You need to `await` the input, and `readLine` doesn't generate a promise directly, so you'll have to "promisify" the callback. There might already be a promisified version of `readLine` in the API (I don't remember off hand.) – kshetline Jul 15 '21 at 13:27
  • sorry I'm really new to js, could you give me an example? I dont really get what youre talking about :D – the_guy71639 Jul 15 '21 at 13:33
  • I actually would just use prompt, but when I use it I somehow can send requests – the_guy71639 Jul 15 '21 at 13:35
  • It looks like someone closed this question, but linking it to a fairly different kind of answer that doesn't help too much here. – kshetline Jul 15 '21 at 13:41
  • Right now I'm just really confused. Would it be possible for you to solve the problem? Because I have no idea how to do so – the_guy71639 Jul 15 '21 at 13:48
  • I'd post some code I've used for this issue before, but it won't fit into a comment, and with the question closed, I have no place to put an answer. – kshetline Jul 15 '21 at 13:53
  • Wait I will open a new one – the_guy71639 Jul 15 '21 at 13:54
  • Oh right I have a cooldown – the_guy71639 Jul 15 '21 at 13:56
  • Maybe you can open one and answer it? Or can I contact you on some other way? – the_guy71639 Jul 15 '21 at 13:57
  • I have some code here in an open source project, starting at line 342, but this is a pretty complex solution (and written in TypeScript, not directly in JavaScript): https://github.com/kshetline/aw-clock/blob/master/build.ts – kshetline Jul 15 '21 at 13:58
  • That answer is more complicated than you need, because I wasn't just doing an input that returns a Promise, but taking over the low-level keyboard handling. – kshetline Jul 15 '21 at 14:01
  • Oh yeah thats to complicated for me. Anyways thank you for you help! Maybe I somewhere find an easier solution – the_guy71639 Jul 15 '21 at 14:06
  • Or do you have an easier one? If so, please just post it somewhere! I really need one – the_guy71639 Jul 15 '21 at 14:25

0 Answers0