Skip to content

Latest commit

Β 

History

History
57 lines (45 loc) Β· 1.13 KB

File metadata and controls

57 lines (45 loc) Β· 1.13 KB

prefer-top-level-await

πŸ“ 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();