prefer-top-level-await
March 27, 2026 ยท View on GitHub
๐ Prefer top-level await over top-level promises and async function calls.
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ก This rule is manually fixable by editor suggestions.
Top-level await is more readable and can prevent unhandled rejections.
Examples
// โ
(async () => {
try {
await run();
} catch (error) {
console.error(error);
process.exit(1);
}
})();
// โ
async function main() {
try {
await run();
} catch (error) {
console.error(error);
process.exit(1);
}
}
main();
// โ
try {
await run();
} catch (error) {
console.error(error);
process.exit(1);
}
// โ
run().catch(error => {
console.error(error);
process.exit(1);
});
// โ
await run();