1

How to sync input with readline function in nodejs? I wanted to take input and store in a variable for later use.

const readline = require('readline');

function createReadObject() {
    read = readline.createInterface( {
        input: process.stdin,
        output: process.stdout
    } );
}

var x;
var read;

createReadObject();
read.question("input :",(input) => { 
    x = input;
    read.close();
} );

console.log(x);

output: input :undefined

It seems readline doesn't wait for the input, rather starts executing later code.

How can make it synchronous with later code? Looking for solution regarding nodejs version 9.4 or previous

Aman Ullah
  • 11
  • 1

0 Answers0