Closed olalonde closed 6 years ago
At a first glance from my phone this seems a little bit like the promisey version of https://github.com/sandfox/generic-pool-decorator ?
Yep, looks like :) Though I really this should be part of node-pool since it's such a common usage pattern.
pool.use = (fn) => {
let resource
return pool.acquire()
.then(r => {
resource = r
return resource
})
.then(fn)
.then((result) => {
pool.release(resource)
return result
}, (err) => {
pool.release(resource)
throw err
})
}
I think in general I'm down with this, I probably won't get much time to do anything about this till the weekend though.
Isn't the general case here that fn
would return a promise itself?
@malthe Yeah. The point is that it would return a Promise
which would complete (reject or resolve) whenever it's done using the resource. Then it doesn't need to remember to release the resource because everything is scope-driven—especially when used with async
/await
.
@malthe Are you confused about something? The example code here works both when fn
returns a Promise
and also when fn
returns something other than a Promise
. See docs for how .then()
works.
The type definition has it like this: use<U>(cb: (resource: T) => U): PromiseLike<U>
.
But I would have expected: use<U>(cb: (resource: T) => PromiseLike<U>): PromiseLike<U>
.
@malthe Looks like the type definitions are coming from the DefinitelyTyped project, not this project. The correct place to report issues about your typings is here. Please see also DefinitelyTyped’s contribution guide.
Would be nice to have a method that automatically acquires and releases a resource, e.g.:
It would be implemented something like this (haven't tested):
Thoughts?