Can HTML script defer execute before script async?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes, a deferred script can execute before an async script. The deciding factor is not the order of the attributes alone, but when each script finishes downloading and whether HTML parsing has already completed.
What defer Means
A script with defer is downloaded in parallel with HTML parsing, but execution is postponed until parsing is complete. Deferred scripts also preserve document order relative to other deferred scripts.
Example:
Even if app.js downloads first, vendor.js still executes before it because deferred scripts run in document order after parsing finishes.
What async Means
A script with async is also downloaded in parallel, but it executes as soon as the download is ready. It does not wait for HTML parsing to finish, and it does not preserve order with other async scripts.
If analytics.js finishes early, it can run in the middle of parsing. If it finishes late, it can run after the document is parsed and after deferred scripts have already executed.
So Can defer Beat async
Yes. Consider this page:
Possible timeline:
- Browser starts parsing HTML
- Both files start downloading
small-app.jsdownloads quickly- HTML parsing finishes
- Browser executes deferred scripts
large-analytics.jsfinishes later and executes after that
In this sequence, the deferred script runs first.
The reverse can also happen:
- Browser starts parsing HTML
large-analytics.jsis actually cached and downloads immediately- Browser executes the async script as soon as it is available
- Parsing continues
- Deferred script waits until parsing is complete
In that case, the async script runs first.
The Correct Mental Model
Do not think of async as "always before" and defer as "always after." Think of them like this:
- '
async: execute as soon as ready' - '
defer: execute after parsing, in document order'
That is why async is good for isolated scripts such as analytics, while defer is good for application code that depends on DOM structure or on other deferred scripts.
Important Limits
These attributes matter only for external scripts with src. Inline scripts do not meaningfully use defer or async in the same way.
Also, if an async script depends on a deferred script, the page is brittle by definition. The browser is allowed to execute them in either relative order depending on network timing.
A Safe Example
Use defer for ordered app files:
Use async for independent telemetry:
This keeps dependency-sensitive code deterministic while letting noncritical tracking load opportunistically.
Common Pitfalls
- Assuming
asyncpreserves script order. It does not. - Loading dependent application bundles with
asyncand then debugging random race conditions. - Assuming a deferred script must always run after an async script. Network timing can make the opposite happen.
- Forgetting that inline scripts do not benefit from
deferthe way external scripts do.
Summary
- A deferred script can execute before an async script if parsing finishes before the async script is ready.
- '
deferwaits for parsing and preserves order among deferred scripts.' - '
asyncruns as soon as it finishes downloading and does not preserve order.' - Use
deferfor dependent app code andasyncfor independent scripts. - The execution race between
asyncanddeferis determined by timing, not by a simple fixed priority.

