What I'm trying to do is to read a file from a path with readline
and return transformed content back to another file which I couldnt solve with other similar topics on SO. If I console.log
logObject
in the module right before return
statement I get exactly what I want - readable object, but in another file I get Promise { <pending> }
. Here's the module I'm creating:
const readline = require('readline');
const fs = require('fs');
// Parse temperature log file into JSON object
const parseTemperature = async filePath => {
let logObject = new Object;
const file = fs.createReadStream(filePath);
const rl = readline.createInterface({
input: file,
})
let iterator = 0;
for await(const line of rl) {
// get date of measurement
let date = line.substring(0, line.indexOf(','));
// get temperature in celcius
let temperature = line.substring(line.indexOf('=')+1,line.length-2);
logObject[iterator] = [date,temperature];
iterator++;
}
console.log(logObject);
return logObject;
}
module.exports = parseTemperature;
Here's how I try to import it in another file (last 2 lines):
const { parse } = require('path');
const path = require('path');
const parseTemperature = require('./src/helpers/parseTemperature');
const os = process.platform;
const scriptsPath = path.join(__dirname, 'scripts');
const logsPath = path.join(__dirname, 'logs');
const temperatureLogsPath = path.join(logsPath, 'temperature.log');
const temperatureValues = parseTemperature(temperatureLogsPath);
console.log(temperatureValues)
What should I change to get readable output in the file where I'm importing module? Or is there another way to read the file without using async await?