0

I have the following code which helps me extract the text from a text_file line by line:

const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
    input: fs.createReadStream('./rooms.txt')
});
// Each new line emits an event - every time the stream receives \r, \n, or \r\n
rl.on('line', (line) => {
    console.log(line);
});
rl.on('close', () => {
    console.log('Done reading file');
});

This code works. However I have been unable to use the line variable outside of its scope in order to pass it to the function that would actually prompt Alexa to say those line. I have tried using a callback function but it did not work. Any suggestions as to how make use of the "line" variable outside of the scope of

rl.on('line', (line) => {
    console.log(line);
});

Grateful!

AG_HIHI
  • 1,705
  • 5
  • 27
  • 69
  • ` I have tried using a callback function but it did not work` show that please. – Jonas Wilms Jul 08 '18 at 12:27
  • @JonasW. Since, I am relatively new to JavaScript, the way I implemented the callback function was really messed-up. So, if you could suggest a way to do it, it would be great :) – AG_HIHI Jul 09 '18 at 09:01
  • Show your code! Otherwise we can't help you. – Jonas Wilms Jul 09 '18 at 09:01
  • You can use a global variable and store whatever you need in there. And then later use it. Although this is not encouraged. – Nikhil Wagh Jul 10 '18 at 09:21
  • @NikhilWagh but the global variable value remains unchanged oustside of the scope of :rl.on('line', (line) => { console.log(line); }); so it's like I did nothing – AG_HIHI Jul 10 '18 at 13:24

0 Answers0