# prefer-top-level-await

Rule `unicorn/prefer-top-level-await`. Prefer top-level await over top-level promises and async function calls.

- Source: unicorn
- Technologies: JavaScript
- Kind: suspicious
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #904 by reach, #1159 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-top-level-await` | no | suggestion | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-top-level-await.md) |
| Oxlint | `unicorn/prefer-top-level-await` | no | no | plugin `unicorn`, category pedantic | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/prefer-top-level-await.html) |

## Code examples

### Incorrect: the rule reports this

```js
(async () => {
  try {
    await run();
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
})();
```

```js
async function main() {
  try {
    await run();
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
}

main();
```

```js
run().catch(error => {
  console.error(error);
  process.exit(1);
});
```

### Correct: the rule accepts this

```js
try {
  await run();
} catch (error) {
  console.error(error);
  process.exit(1);
}
```

```js
await run();
```

```js
const preparationDone = prepareSomething();

export async function doSomething() {
  await preparationDone;
}
```

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-top-level-await": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/prefer-top-level-await": "error"
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-string-raw](https://lintradar.dev/rules/unicorn/prefer-string-raw.md): Prefer using the `String.raw` tag to avoid escaping `\`.
- [unicorn/better-dom-traversing](https://lintradar.dev/rules/unicorn/better-dom-traversing.md): Prefer better DOM traversal APIs.
- [unicorn/no-useless-iterator-to-array](https://lintradar.dev/rules/unicorn/no-useless-iterator-to-array.md): Disallow unnecessary `.toArray()` on iterators.
- [unicorn/no-canvas-to-image](https://lintradar.dev/rules/unicorn/no-canvas-to-image.md): Prefer drawing canvases directly instead of converting them to images.
- [unicorn/prefer-global-this](https://lintradar.dev/rules/unicorn/prefer-global-this.md): Prefer `globalThis` over `window`, `self`, and `global`.
- [unicorn/no-collection-bracket-access](https://lintradar.dev/rules/unicorn/no-collection-bracket-access.md): Disallow accessing `Map`, `Set`, `WeakMap`, and `WeakSet` entries with bracket notation.

---

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