Async Script Execution Order
Quick summary
Your async scripts execute as soon as they download, potentially out of order — if script B depends on script A, B may run before A.
Why this happens
Using async on scripts that depend on each other
Mixing async and defer incorrectly
Assuming scripts execute in the order they appear in the HTML
Minimal example
<!-- async scripts execute in download order, not DOM order -->
<script src="library.js" async></script>
<script src="app.js" async></script>
<!-- If app.js loads first, it fails because library.js is not ready --><!-- defer scripts execute in DOM order after parsing -->
<script src="library.js" defer></script>
<script src="app.js" defer></script>
<!-- app.js always runs after library.js -->async executes scripts as soon as they download, in any order. defer downloads in parallel but executes in order, guaranteeing library.js runs before app.js.
How to diagnose
Check which scripts have the async attribute
Look for errors like 'X is not defined' that indicate dependency issues
Use the Network tab to see download and execution order
How to fix
Use defer instead of async for scripts that depend on each other — defer preserves order
Bundle dependent scripts into a single file
Use dynamic import() for explicit dependency management
How to prevent
Use defer for non-critical scripts that need ordered execution
Use async only for independent scripts (analytics, tracking pixels)
Bundle scripts with a build tool to manage dependencies
Related Resources
Related Lessons
- Web Performance
Script loading and performance.