title: Make a readln function finish before executing next function that uses that data in NodeJS
body: <p>please some help with this. I want to make a function that creates a nx2 matrix using console inputs (input lines of two spaced integers). The problem is when I want to make use of this data from another function, my data seems to be undefined. For example, the printmatrix function calls the readfunction but results in undefined because it seems that the readfunc has not yet read all the keystrokes yet, and the printmatrix function is already executed with no input data. How I can make sure the function that requires user input finishes before executing the print function or any other function that would require the data to be ready to be read? Thank you for your help.</p>
  <pre><code>const readline = require('readline');
  
  const readfunc =  () =&gt;{
      const rl = readline.createInterface({
          input: process.stdin,
          output: process.stdout
      });
  
      let arr=[];
      let mat=[];
  
      rl.on('line', (input) =&gt; {
      
      let splitAns=input.split(&quot; &quot;);
      arr.push(splitAns[0])
      arr.push(splitAns[1])
      mat.push(arr)
      arr=[];
  
    });
  
    rl.on('close', () =&gt;{
        return (mat)
    })
  }
  
  const printmatrix =  () =&gt;{
  
      const result =  readfunc();
      console.log(result);
  }
  
  printmatrix(); // prints undefined :(
  </code></pre>