Resource to make disposable.
Function to call when disposing of the resource.
Disposable version of the resource.
import { disposable } from "@pacote/disposable"
import fs from "node:fs"
const getFileHandle = (path: string) => {
const descriptor = fs.openSync(path)
return disposable(
{
read: (buffer) => fs.readSync(descriptor, buffer),
},
() => fs.closeSync(descriptor),
};
};
{
using fileHandle = getFileHandle("file.txt");
fileHandle.read(buffer)
// descriptor is automatically closed
}
Creates a version of the resource that can be disposed of at the end of a function block.