Skip to content

topLevelAwaits

Reports top-level await expressions in files that export values.

✅ This rule is included in the ts logicalStrict presets.

Top-level await (“TLA”) allows using await at the module level. While convenient for standalone files run like scripts, it can cause issues if those files are imported by other modules:

  • Modules using top-level await block their dependents until the await resolves
  • This can cause unexpected delays in application startup
  • It makes module loading order slower and sometimes less predictable
const
const config: any
config
= await import("./config.json");
export const
const apiUrl: any
apiUrl
=
const config: any
config
.
any
apiUrl
;
const
const data: any
data
= await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
fetch
("/api/data").
Promise<Response>.then<any, never>(onfulfilled?: ((value: Response) => any) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<any>

Attaches callbacks for the resolution and/or rejection of the Promise.

@paramonfulfilled The callback to execute when the Promise is resolved.

@paramonrejected The callback to execute when the Promise is rejected.

@returnsA Promise for the completion of which ever callback is executed.

then
((
r: Response
r
) =>
r: Response
r
.
Body.json(): Promise<any>
json
());
export {
const data: any
export data
data
};

This rule is not configurable.

If you’re intentionally using top-level await for module initialization and understand the implications for module loading, you may disable this rule. You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.

Made with ❤️‍🔥 around the world by the Flint team and contributors.