Possibility for Disposer chaining
See original GitHub issue(This issue tracker is only for bug reports or feature requests, if this is neither, please choose appropriate channel from http://bluebirdjs.com/docs/support.html)
Please answer the questions the best you can:
-
What version of bluebird is the issue happening on? 3.5
-
What platform and version? (For example Node.js 0.12 or Google Chrome 32) irrelevant
-
Did this issue happen with earlier version of bluebird? probably
I want to achieve some kind of disposer chaining. I have one resource for its construction another resource is needed. So my resource must be disposed before source resource is disposed.
function getClient() {
return pg.connect(connString).disposer(function(client) {
console.log('CLIENT RELEASE');
client.release();
});
}
// getter for depended resource
function getLOM() {
return Promise.using(getClient(), function(client) {
return Promise.resolve(new LargeObjectManager(client)).disposer(function(lom) {
lom.close();
});
});
}
// desired usage
Promise.using(getLOM(), function(lom) {
// do something
});
The problem is that client
is disposed before LargeObjectManager (dependent object). I am aware of that I can rewrite it using callback but this is not exactly what I want.
Fallback solution
function useLom(fn) {
return Promise.using(getClient(), function(client) {
return Promise.using(Promise.resolve(new LargeObjectManager(client)).disposer(function(lom) {
lom.close();
}), fn);
});
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:13 (1 by maintainers)
I think this idea makes sense, but the API needs to be thought out. We can reopen if there is a good and not overly complex API proposal for it? Perhaps something like this?
Although I suppose it would also need to support deriving from multiple other disposers, so maybe a static method taking one or more arguments like
Promise.using
would be better.Here’s what I came up using only public methods:
Usage: