8000
lock('key', function(unlock) { unlock()(); })
Currently, this is too convoluted. It should be:
lock('key', function(unlock) { unlock(); })
your example code is misleading because you are not doing anything that is async.
when you use it as a callback, as given in the examples, it is not as bad.
lock(key, function(unlock) { fs.readFile(key, unlock()) })
In the age of async / await this feels a little odd. I had to do this:
await new Promise((resolve, reject) => { lock('key', async unlock => { await asyncFunction() unlock()(); resolve(); }); });
It would be awesome if the callback could await a promise before releasing. Then I could do this:
await lock('key', async () => { await asyncFunction() });
@alflennik we promisify it like this:
function promisifiedLock () { const lock = Lock() return (key) => new Promise((resolve) => { lock(key, (release) => { resolve(promisifyRelease(release)) }) }) } function promisifyRelease (release) { return () => new Promise((resolve, reject) => { release((err) => { if (err) { reject(err) } else { resolve() } })() }) }
then use it like this:
const lock = promisifiedLock() const release = await lock('key') try { await asyncFunction() } finally { await release() }
Activity
dominictarr commentedon May 2, 2016
your example code is misleading because you are not doing anything that is async.
when you use it as a callback, as given in the examples, it is not as bad.
alflennik commentedon Apr 9, 2018
In the age of async / await this feels a little odd. I had to do this:
It would be awesome if the callback could await a promise before releasing. Then I could do this:
poislagarde commentedon May 30, 2018
@alflennik we promisify it like this:
then use it like this: