I have a simple script that processes through two large files. What I'm trying to achieve is to parse through the first file first.txt then pass through the second file second.txt and then collate the information into the out.txt file.
Here's my code so far:
var fs = require('fs'),
readline = require('readline');
var out = fs.createWriteStream('./out.txt', { 'flags' : 'a' });
var rl = readline.createInterface({
input: fs.createReadStream('./first.txt'),
output: process.stdout,
terminal: false
});
rl.on('line', function(line) {
// get data now from other file
info = getData( line, function( err, data ) {
if ( err ) console.log( err );
return data;
});
out.write( line + " " + info );
});
rl.one('close', function() {
console.log("Finished!");
});
function getData( searchText, callback ) {
src = readline.createInterface({
input: fs.createReadStream('./second.txt'),
output: process.stdout,
terminal: false
});
src.on('line', function(line) {
var findText = line.substring(0, searchText.length);
if ( line === findText ) {
src.close();
callback( null, line.substring( findText.length ).trim() );
}
});
}
I've tried inserting a var timer = setTimeout( function() { callback('timeout!') }, 10000 ); in the getData function to allow time for the function to process, however, the setTimeout never seems to get called.
Anyway, even with the above code all I get is undefined even though I can confirm (through console.logs that text can be returned from the getData function.
What am I doing wrong?
My first tipp would be instead:
-- callback( null, line.substring( findText.length ).trim() );
use:
++ return callback( null, line.substring( findText.length ).trim() );