Skip to content

mstuart/using-safe

Repository files navigation

using-safe

Safely use and dispose resources, even without the using declaration

Install

npm install using-safe

Usage

import usingSafe from 'using-safe';

const result = await usingSafe(resource, async (r) => {
	return await r.doWork();
});
// Resource is automatically disposed

API

usingSafe(resource, function_)

Runs function_ with resource, then disposes resource in a finally block.

Disposal order: Symbol.asyncDispose -> Symbol.dispose -> .close() -> .destroy()

Returns the result of function_.

usingSafeSync(resource, function_)

Synchronous version.

Disposal order: Symbol.dispose -> .close() -> .destroy()

Returns the result of function_.

Related

  • disposable-from - Create Disposable wrappers for timers, event listeners, intervals, and custom cleanup

License

MIT

About

Safely use and dispose resources, even without the using declaration

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors