0

Thought I make myself a module (yesno) to get a users confirmation. Used the node module prompt for that purpose.

const prompt = require('prompt');

const yesnoOptions = {
    properties: {
        confirm: {
            pattern: /^yes|no|ja|nee$/i,
            message: 'Antwoord met ja of nee.',
            required: true

        }
    }
};

async function confirm(){
    prompt.start();
    prompt.get(yesnoOptions, function(err, result){
        if (err){
            return err;
        }
        if ( String(result.confirm).match(/^yes|ja$/i) ){
            return 1;
        } else{
            return 0;
        }
    });
}

module.exports = {
    confirm: confirm,
}

Whenever I use the function it continues with the next line before the user input is received.

            let reply = await yesno.confirm();
            console.log(`reply ${reply}`);

The console.log prints "reply undefined" and I'm left at an empty line prompt waiting for input.

Tried without the await: in that case the console.log wil print "reply [object Promise]"

Tried chaining the result (not at all familiar with that technique) but the .then() is executed immediatelly.

Did use await and async functions at other moment. Worked as expected.

I'm at a loss... could use a pointer on what I'm doing wrong.

Regards

Peter

Peter
  • 23
  • 6

0 Answers0