# prefer-dispose

Rule `unicorn/prefer-dispose`. Prefer using `using`/`await using` over manual `try`/`finally` resource disposal.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2029 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-dispose` | no | suggestion | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-dispose.md) |

## Code examples

### Incorrect: the rule reports this

```js
const file = open();
try {
  read(file);
} finally {
  file.close();
}
```

```js
const reader = openReader();
const writer = openWriter();
try {
  pipe(reader, writer);
} finally {
  writer.close();
  reader.close();
}
```

```js
async function run() {
  const connection = await connect();
  try {
    await query(connection);
  } finally {
    await connection.close();
  }
}
```

### Correct: the rule accepts this

```js
{
  using file = open();
  read(file);
}
```

```js
{
  using reader = openReader();
  using writer = openWriter();
  pipe(reader, writer);
}
```

```js
async function run() {
  await using connection = await connect();
  await query(connection);
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-dispose": "error",
    },
  },
];
```

## Related rules

- [unicorn/no-unused-properties](https://lintradar.dev/rules/unicorn/no-unused-properties.md): Disallow unused object properties.
- [unicorn/prefer-error-is-error](https://lintradar.dev/rules/unicorn/prefer-error-is-error.md): Prefer `Error.isError()` when checking for errors.
- [unicorn/prefer-iterator-concat](https://lintradar.dev/rules/unicorn/prefer-iterator-concat.md): Prefer `Iterator.concat(…)` over temporary spread arrays.
- [unicorn/no-unsafe-dom-html](https://lintradar.dev/rules/unicorn/no-unsafe-dom-html.md): Disallow unsafe DOM HTML APIs.
- [unicorn/prefer-json-import](https://lintradar.dev/rules/unicorn/prefer-json-import.md): Prefer JSON imports over reading and parsing JSON files.
- [unicorn/no-unreadable-new-expression](https://lintradar.dev/rules/unicorn/no-unreadable-new-expression.md): Disallow unreadable `new` expressions.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-dispose. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
