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();