π 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.
// β
(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();Initializing a variable directly with a top-level promise for later awaiting is allowed:
// β
const preparationDone = prepareSomething();
export async function doSomething() {
await preparationDone;
}