2

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();
0

1 Answer 1

2

your function asyncForEach and the callback of your asyncForEach call in getContentFromEachFile don't return a promise so you can't use it with async/await functions.

Async/await is not required for getContentFromEachFile()

As a result, I would do :

function asyncForEach(array, callback) {
  return new Promise(async (resolve, reject) => {
    let result = []
    array.forEach((file, index, files) => {
      // concat the callback returned array of each file into the result
      const res = await callback(file, index, files);
      result = result.concat(res);
    });
    return resolve(result);
  });
};

function getContentFromEachFile(filePath) {
  return readline.createInterface({
    input: fs.createReadStream(filePath),
    crlfDelay: Infinity
  });
};

async function readSeoDataFromEachFile() {
  return await asyncForEach(files, (file) => {
    return new Promise((resolve, reject) => {
      const filePath = path.join(__dirname + '/' + folderName, file);
      let callbackResult = [];
      const rl = getContentFromEachFile(filePath);

      rl.on('line', async (line) => {
        let data = performSeoDataSetUpProcess(line);
        console.log(JSON.stringify(data.obj));

        // add the result from redis into the generated data
        data.redisResult = await getResultsFromRedisUsingKey(data.obj.name);
        console.log("result" + JSON.stringify(data.redisResult));

        // store the result in the local variable
        callbackResult.push(data);
      });

      rl.on('close', () => {
        // finally return the stored result for this file
        return resolve(callbackResult);
      });
    });
  });
};

console.log(readSeoDataFromEachFile());
Sign up to request clarification or add additional context in comments.

12 Comments

getContentFromEachFile function returning each file content but problem is not able to call getResultsFromRedisUsingKey result from this function
Are you sure getResultsFromRedisUsingKey() function returns a promise ?
Sorry i did not added getResultsFromRedisUsingKey function on question, Now i have added Please check once again question.
What is logged on data.obj and result ?
Thanks @dun32 you helpped me a lot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.