DEV Community

Cover image for Async and defer, the difference.
Thomas Bnt
Thomas Bnt Subscriber

Posted on • Edited on

3 2

Async and defer, the difference.

  • Introduction

I recently had this problem with JavaScript files that slowed down a web page as soon as it was loaded. It's not nice to wait, I understand. That's why I looked at the history of async and defer.
Two different load modes, one that loads during the execution of the JS and the other that wait for everything to be loaded for the JS to run at the end.

  • Explanation of the different execution modes

As said before for the async, it loads during the analysis of HTML but also the execution of JavaScript. So he loads all of a sudden.
While defer, patiently waiting for the page loading and analysis of HTML, it runs at the last moment.

Schema of Async

Code example :

<script type="text/javascript" src="assets/js/init.js" async></script>
Enter fullscreen mode Exit fullscreen mode

Schema of Defer

Code example :

<script type="text/javascript" src="assets/js/init.js" defer></script>
Enter fullscreen mode Exit fullscreen mode
  • Without both, what's going on?

So you do not have to put these two forms of loading although it is useful, if you do not put anything in your code that will recover the JS resource, it will run at the same time as loading.

  • Credits

    • cover_image rendered with random-background-image-with-unsplash and the spin from Fork Awesome
    • Post was written on Busy a few months ago and is written in French.
Check my Twitter account. You can see many projects and updates. You can also support me on Buy Me a Coffee, Stripe or GitHub Sponsors. Thanks for read my post ! 🤩

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs