ronomon / opened

Check if a file is open in another application on Windows, macOS and Linux.
MIT License
17 stars 7 forks source link

Does not work with util.promisify... however: #1

Closed alt-jero closed 6 years ago

alt-jero commented 6 years ago

Trying to use with Promisify library may produce the following error if any files are indeed open:

Uncaught TypeError: Cannot read property 'unescape' of undefined
    at //node_modules/@ronomon/opened/index.js:106:31

even if it does work with no files open. Thus: the following code will allow you to use this with promises:

// const openedFiles = promisify(Opened.files) // For if/when it does work...
const openedFiles = paths => {
    return new Promise( (resolve, reject) => {
        Opened.files(paths, (error, hashTable) => {
            if (error) return reject(error)
            return resolve(hashTable)
        })
    })
}

and can be used like so:

// Using ES6 syntax
let paths = [...]
// async/await method (Use only within function/method marked async)
let openPaths = await openedFiles(paths).catch(error => throw error)
// promise method
openedFiles(paths)
  .then(pathTable => openPaths = pathTable )
  .catch(error => throw error)
jorangreef commented 6 years ago

Thanks @alt-jero , I hope your workaround will help others who want to use promises.

Supporting promises is a non-goal however. I would rather encourage vanilla JS. It's more efficient from a performance perspective.