q2s2t / node-7z

A Node.js wrapper for 7-Zip
https://www.npmjs.org/package/node-7z
ISC License
180 stars 60 forks source link

Getting ".then is not a function #64

Closed sidprice closed 5 years ago

sidprice commented 5 years ago

I am using node-7z together with 7zip-bin to sequentially process some archive files in my Electron (node.js) application. The issue I have is that I get the above error when I try to run my code. I have searched for a solution but so far been unable to find one. The code in questions is:

function processArchive(pdscQueue, archiveFilename, repoPath) {
    let tempFolder = tmp.dirSync() ;
    let devicePackFile = path.join(repoPath,archiveFilename) ;   // Create a temp folder for the extacted data
    sevenZip.extractFull(devicePackFile, tempFolder.name, { $bin: pathTo7zip})

    .then( function () {
        //
        // Process this file
        //
        tempFolder.removeCallback() ;   // Clean up
        //
        // Check if there are more to process
        //
        if ( pdscQueue.isEmpty()) {
            return ;
        } else {
            processArchive(pdscQueue, pdscQueue.dequeue(), repoPath ) ;
        }
    });
}

I hope you can help me, thanks for this nice package.

m59peacemaker commented 5 years ago

.then is for a promise api, which is not implemented in this library. You can create your own promise and call the 7z function in the constructor, and resolve it on the end event and reject on the error event.

new Promise((resolve, reject) => {
    const files = []
    const process  = sevenZip.extractFull(devicePackFile, tempFolder.name, { $bin: pathTo7zip})
    process.on('data', file => files.push(file))
    process.on('end', () => resolve({ files }))
    process.on('error', reject)
})
  .then(etc) // now this works

Here's some code where I promisified the whole library and have it automatically pass $bin for me. https://github.com/m59peacemaker/extract-archive-without-blowing-up/blob/master/7z.js

sidprice commented 5 years ago

Thanks for the update, I had already found this route and the library is working nicely, nice work.