Search code examples
requirejsdojoamd

need to understand AMD behavior in dojo


I need to understand one behavior of AMD in dojo. In the below example will statement 1 be always executed first and then statement 2 if ready or domReady! not used?

 function test() { 
    var abc;  
    require(["dijit/registry"], function(registry){ 
    //some modification of abc variable.
     console.log("statement 1");----> statement 1 
    });  
    return abc;----> statement 2  
    }

Thanks in advance.


Solution

  • Nope... statement 1 will be fired once dijit/registry has been loaded. There is no guarantee that this will be the case when you reach statement 2.

    Only the statements inside your require callback are ensured to fire in order.

    The above is valid even if you use ready or domReady!

    You do try the following to expose your function globally :

    require(["dojo/_base/kernel", "dijit/registry"], function(kernel, registry){
        kernel.global.test = function(){
            var abc;
            //some modification of abc variable.
            console.log("statement 1");----> statement 1
            return abc;----> statement 2  
        }
    });