tfoxy / chrome-promise

Promises for chrome JavaScript APIs used in extensions and apps.
MIT License
150 stars 14 forks source link

Optionally replace existing methods #3

Closed ngryman closed 8 years ago

ngryman commented 8 years ago

Hi,

It would be nice to be able to use directly the promisifyied api instead of accessing it through the chrome.promise namespace. I understand that this is more safe this way as it allows existing code using old-style callbacks to continue to work. But if your entire project uses the promisifyied version, this is kind of cumbersome to have to type chrome.promise.

Do you think you could add an option to do this, or even make this behavior the default one and add an option to namespace promisifyied methods if needed?

Thanks!

tfoxy commented 8 years ago

Hi,

This library creates a global variable called ChromePromise. chrome.promise is only used on the readme and is initialized using chrome.promise = new ChromePromise();.

With window.chrome = new ChromePromise(); you can achieve what you want.

Maybe I should clarify the examples using something like chromep instead of chrome.promise.

ngryman commented 8 years ago

Oh right, I've been too quick on this one. Sorry for this.

Yes you should perhaps make this as the default usage in your doc as most users might only want to use the promisifyied version of the API:

window.chrome = new ChromePromise()

On a side note, it feels weird to override the chrome object itself, it would feel more natural if your API was not a constructor returning a new object, but a function that replace all methods with the promisifyied versions of it, returning the patched object instead (much like bluebird does):

// would patch `chrome` by default
ChromePromise()
// explicit
ChromePromise(chrome)
// custom namespace
chrome.promise = ChromePromise()
// bonus: only a subset of the API
ChromePromise(chrome.bookmarks)