0

I have a working NodeJs code to extract a .zip file and extract it to a particular directory location,
And read the contents of the extraction directory location.

I am using the async await syntax, as I need both the extraction and read operations to happen one after another,
But for some reason, the code is not synchronous and the read operations don't always show the extracted files in console logs.

Following is the code -

const extract = require('extract-zip');
const util = require('util');
const fs = require('fs');

let test = async () => {
    await extract('./Archive.zip', {dir: '/somePath/temp'}, function (err) {
        if(err){
            console.log('Error => ', err);
        }
    });


    let fsReaddir = await util.promisify(fs.readdir).bind(fs);
    let readDirResult = await fsReaddir('/somePath/temp');

    await readDirResult.forEach(function (file) {
        console.log(file);
    });
}

test();

What should be the correct implementation?

Dev1ce
  • 5,390
  • 17
  • 90
  • 150

0 Answers0