This package allows you to use C# like syntax for resource management.
const {Resource, using} = require('use-resource')
console.log('Creating resource')
const r = new Resource({}, async () => console.log('Disposing resource'))
console.log('Using resource')
using(r, async r => {
const timeout = new Promise(resolve => setTimeout(() => resolve(), 1000))
await timeout
console.log('Working with resource')
})
npm install use-resource
const {Disposable, using} = require('use-resource')
class MySshTunnel extends Disposable {
async dispose () {
console.log('Closing ssh tunnel')
}
}
using(new MySshTunnel(), async tunnel => {
console.log('Doing stuff using the ssh tunnel')
}