Pacote
    Preparing search index...

    Function disposable

    • Creates a version of the resource that can be disposed of at the end of a function block.

      Type Parameters

      • T extends object

      Parameters

      • resource: T

        Resource to make disposable.

      • callback: () => void

        Function to call when disposing of the resource.

      Returns T & Disposable

      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
      }