Safely use and dispose resources, even without the
usingdeclaration
npm install using-safeimport usingSafe from 'using-safe';
const result = await usingSafe(resource, async (r) => {
return await r.doWork();
});
// Resource is automatically disposedRuns function_ with resource, then disposes resource in a finally block.
Disposal order: Symbol.asyncDispose -> Symbol.dispose -> .close() -> .destroy()
Returns the result of function_.
Synchronous version.
Disposal order: Symbol.dispose -> .close() -> .destroy()
Returns the result of function_.
- disposable-from - Create Disposable wrappers for timers, event listeners, intervals, and custom cleanup
MIT