Skip to content

Node module to add a C# like syntax for resource management.

Notifications You must be signed in to change notification settings

feramhq/use-resource-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Resource Management with 'using'

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')
})

Installation

npm install use-resource

Custom disposable classes

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')
}

About

Node module to add a C# like syntax for resource management.

Resources

Stars

Watchers

Forks