0

I am using createReadStream of file module. I have a async operation which is reading from redis/db and based on saved value, I have to increment different counters. This is done inside the stream. The problem is end event is called on stream before the reading and increment operation have been completed. I have to update the counter based on status store in redis/db. I have tried using async/await but nothing work.

    let fileStream = fs.createReadStream(process.env.FILE_PATH + file_name, { highWaterMark: 1024 * 1024 });
    fileStream.pipe(es.split()).pipe(
        es.mapSync(async function (line) {
            totalCount++;
            let extractData = line.split(",");
            let number = extractData[0];

            if (number) {
                number = number.replace(/['"]+/g, "");

              // HERE IT Check in redis or db
                const status = await checkNumberStatus(number, shortCode);

                tempStatus = status; 

                if (status === "unsub") {
                    unprocessCount++;
                } else {
                       // add in redis 
                    addToCronJob(number, campaign).then(() => {
                        processCount++;
                    });
                }
            }
        })
    );
    
    

 
    fileStream.on('end',async function (){
       

        campaign.cron_end_time= new Date();
        campaign.process_count= tempStatus === 'unsub'? processCount: ++processCount;
        campaign.total_count= totalCount;
        campaign.unprocess_count = tempStatus === 'unsub'? ++unprocessCount: unprocessCount; 
        await campaign.save();
    })
osama
  • 116
  • 1
  • 9

0 Answers0