fresc81 / node-winreg

node module that provides access to the Windows Registry through the REG commandline tool
211 stars 58 forks source link

Promises #38

Open LukeTim opened 7 years ago

LukeTim commented 7 years ago

I am finding this library quite hard to use with all the asynchronous callbacks. I am trying to compile a list of installed software and patches which requires looping through multiple keys and stepping down into each one to retrieve multiple subkeys. Therefore I need some way to guarantee I can append all this data to my lists after all callbacks are completed. So, is there anyway I can do this? Does this library perhaps support promises?

fresc81 commented 7 years ago

Promises are not yet supported. But I like the idea. In the meantime maybe https://github.com/caolan/async would come in handy. I usually use it when dealing with asynchronous code.

RX14 commented 7 years ago

There's always bluebird's promisify. I found it hard to use promises at all without bluebird, but that was over a year ago...

fresc81 commented 7 years ago

Yeah, looks like you could even use bluebird to promisify other Promise unaware APIs like winreg's for example. This probhably suits your usecase even more, @LukeTim . http://bluebirdjs.com/docs/features.html#promisification-on-steroids

fresc81 commented 7 years ago

Would be great if you can give some feedback if bluebirdjs works in combination with winreg.

LukeTim commented 7 years ago

So it's been a while but I've finally got some down time to take another look at this app. I tried using bluebird but it doesn't seem to work. I just get told that "keysAsync" doesn't exist.

I might give async a go, but I am not sure it will help. I need a way to have a piece of code that is guaranteed to run after all of my calls to .get() are completed, so that I can build an object and push it on to an array.

fresc81 commented 7 years ago

Thanks for your feedback. Would aliases for the winreg functions help in this case? e.g. Registry.prototype.getAsync = Registry.prototype.get You should give async definitely a try. Performing async tasks and then call callbacks ( async.parallel ) is basically what it's useful for.

dkudrin commented 6 years ago

@fresc81 It works only this way:


import { promisifyAll } from 'bluebird'
let regKey = new Registry({
      hive: Registry.HKCU,
      key:  '\\Software\\Microsoft\\Windows\\CurrentVersion\\Run'
    })

promisifyAll(regKey)

let vals = await regKey.valuesAsync()
console.log(vals)

Even promisify(regKey.values)() does not work.

tgds commented 6 years ago

@fresc81, first of all, thank you for the library, works great! I prefer working with Promises as well, so I took the liberty, forked your project and added support for native Promises (node >= 0.12.18). Each method now returns a Promise if no callback is specified.

Let me know if you'd be interested in merging these changes. I'd send you a pull request, but it seems messy as there are some issues with whitespace and the diffs are large, so I'm asking here first before I start cleaning it up. Perhaps, there's still some work needed on the documentation part as well.

LukeTim commented 6 years ago

@dkudrin Thank you so much for that. I managed to get promises to work with bluebird now. An absolute godsend. This project had been sitting on the back burner for months and now I'm making a lot of progress.