Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt

Use this file to discover all available pages before exploring further.

Use top-level await in ES modules instead of wrapping async code in an IIFE or a named async function that is immediately called. Top-level await is more readable and lets the runtime surface unhandled rejections reliably.
Recommendedrecommended · ☑️ unopinionated
Suggestions💡 Has suggestions
.cjs files are ignored by this rule.
This rule flags three patterns at the top level of a module:
  1. Promise chains.then(), .catch(), or .finally() called directly on a promise.
  2. Async IIFEs(async () => { … })().
  3. Named async function callsasync function main() { … }; main().

Examples

// Async IIFE
(async () => {
  try {
    await run();
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
})();

// Named async function called immediately
async function main() {
  try {
    await run();
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
}

main();

// Promise chain
run().catch(error => {
  console.error(error);
  process.exit(1);
});
Top-level await is only available in ES modules. If your file uses CommonJS (.cjs extension or no "type": "module" in package.json), this rule will not apply.

Build docs developers (and LLMs) love