How can I use await function under readline.on function on nodejs
I'm trying to read each line using readline.on function and after reading each line from file, i'm trying to pass each line data to some other function which is an third party api so I have written promise for that function so calling that function using await under readline.on function, but its not returning result from that function. Can any one please help me to solve this one. Thanks in advance.
"use strict";
import yargs from 'yargs';
import fs from 'fs';
import redis from 'redis';
import path from 'path';
import readline from 'readline';
const args = yargs.argv;
// redis setup
const redisClient = redis.createClient();
const folderName = 'sample-data';
// list of files from data folder
let files = fs.readdirSync(__dirname + '/' + folderName);
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
async function getContentFromEachFile(filePath) {
return new Promise((resolve, reject) => {
let rl = readline.createInterface({
input: fs.createReadStream(filePath),
crlfDelay: Infinity
});
resolve(rl);
});
};
async function readSeoDataFromEachFile() {
await asyncForEach(files, async (file) => {
let filePath = path.join(__dirname + '/' + folderName, file);
let rl = await getContentFromEachFile(filePath);
rl.on('line', async (line) => {
let data = performSeoDataSetUpProcess(line);
console.log(JSON.stringify(data.obj));
let result = await getResultsFromRedisUsingKey(data.obj.name);
console.log("result" + JSON.stringify(result));
});
});
};
async function getResultsFromRedisUsingKey(key) {
return new Promise((resolve, reject) => {
redisClient.get(key, function (err, result) {
if (err) {
resolve(err);
} else {
resolve(result);
}
});
});
};
readSeoDataFromEachFile();