ORESoftware/live-mutex

API change recommendation

Closed this issue · 2 comments

You don't need 2 methods for lock with cb and promise. You can check whether the cb is present and if not, then return a promise. This is a little bit more error prone, but makes the api simpler. For example it is used here: https://github.com/jprichardson/node-fs-extra Another idea is to use utils.promisify if you keep the current api. https://nodejs.org/api/util.html#util_util_promisify_original And ofc it would be nice to see async-await examples with these promises...

Thanks - I found that with static typing (typescript), it's cleaner to use two separate methods - one for promises, one for vanilla callbacks. I have another project where I had a conditional promise/callback, but I moved it to two separate methods. But I know what you mean. Let me know if the library works for you.

If you look at the live-mutex codebase, the callback only implementation is already complex, because of the absolute need for retries. The simplest way to implement promises, is to simply wrap the callback implementation. And I actually think it's more performant that way as well, otherwise you create a new promise for every retry, if you follow.

I added a section in the docs describing usage with async/await

https://github.com/ORESoftware/live-mutex#to-use-with-asyncawait-it-simply-looks-like