TarefaAdd three scripts to the head: /analytics.js as async, /utils.js as defer, /app.js as defer. Order matters for defer scripts.
defer vs async (o atributo de script correto)
75 XP7 min
Teoria
When to choose which
| Attribute | Download | Execute | Order preserved | Use for |
| (none) | blocking | immediately | yes | almost never |
async | parallel | as soon as downloaded | NO | analytics, ads, isolated 3rd-party |
defer | parallel | after HTML parsed | YES | your app code, dependency chains |
<!-- BAD — blocks page render --> <script src="/analytics.js"></script> <!-- GOOD — analytics doesn't depend on anything --> <script src="/analytics.js" async></script> <!-- GOOD — your app code, runs in source order --> <script src="/utils.js" defer></script> <script src="/components.js" defer></script> <script src="/app.js" defer></script>
Why "order preserved" matters
defer scripts run in source order — utils.js finishes before app.js starts. async runs whichever finishes downloading first — app.js might execute before utils.js, breaking your dependency chain.
ES modules are deferred by default
<script type="module" src="/main.js"></script>
type="module" implies defer. No need to add it. Modules also automatically run in source order.
Inline scripts
async/defer only work on EXTERNAL scripts (src="..."). Inline <script> blocks always run synchronously when parsed.
🔒
Regista-te para começar a programar
A teoria está aberta a todos. O editor, a pré-visualização e o check desbloqueiam-se com um teste gratuito de 7 dias — cartão obrigatório, cancelável a qualquer momento.
Registar — teste grátis →As primeiras 10 lições de cada trilha são grátis. Sem cartão necessário.