Question

I am using AMD way of coding and when I make async:false I don't see any difference in execution behavior i.e. making async true or false I see no difference.

Please have a look at code snippet below:

<html>
<script type="text/javascript" src="../../dojoroot/dojo/dojo.js" data-dojo-config="async:true, parseOnLoad:true"></script>

<script type="text/javascript">
require(["dojo/domReady!"], function(){
alert("I am inside DOM ready");
});

require(["dojo/ready"], function(ready){
    ready(function(){
    alert("I am inside ready");
  });
});
</script>
<body>
some div's here
</body>
</html>

OUTPUT: In both the case i see first "I am inside DOM ready" and then "I am inside ready" alert next.

Could you please explain what is the difference if I add async : true and async:false?

Thanks.

Was it helpful?

Solution

To answer your question, if async is set to a truthy value (true, 1, etc.), dojo/domReady and dojo/ready will be loaded when require is called. If async is not set, these modules are loaded immediately when dojo.js is loaded. So there is no impact on how your code executes, it’s just about when those modules become available for use.

This is snippet from Dojo AMD description:

For backwards-compatibility, the loader starts up in synchronous mode by default, which loads the Dojo base API automatically:

<script src="path/to/dojo/dojo.js"></script>
<script>
  // the dojo base API is available here
</script>

To put the loader in the AMD mode, set the async configuration variable to truthy:

<script data-dojo-config="async: 1" src="path/to/dojo/dojo.js"></script>
<script>
  // ATTENTION: nothing but the AMD API is available here
</script>

Note that you can only set the async flag before dojo.js is loaded, and that in AMD mode, neither Dojo Base nor any other library is automatically loaded - it is entirely up to the application to decide which modules/libraries to load.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top