What is a concise way to implement nested disposers?


What is a concise way to implement nested disposers?



Picture a typical resource returned by a method getResource that has a disposer, that you would use like this:


getResource


Promise.using(
getResource(),
resource =>
doStuffWith(resource))
.then(
// "resource" is cleaned up by now
)



Now imagine another method wrapResource that took the resource as an argument and returned it decorated in some way (perhaps adding its own initialize/teardown steps) - perhaps with its own disposer. You could use such a method like this:


wrapResource


Promise.using(
getResource(),
baseResource =>
Promise.using(
wrapResource(baseResource),
resource =>
doStuffWith(resource)))



My question is whether there is a way to write the above in a more succinct way that abstracts away the wrapping of that resource, something you could use like this:


Promise.using(
getWrappedResource(),
resource =>
doStuffWith(resource))



In other words - how could one implement getWrappedResource?


getWrappedResource









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy