Monday, January 20, 2014

Testing synchronous vs asynchronous Dojo 1.9

I have this in a.js:
require(["dojo/_base/xhr"], function(){
  console.log("a");
});

I have this in b.js:
define(["dojo/_base/xhr"], function(){
  console.log("b");
  return {};
});

And I run this (in index.html):
console.log("TESTING Part One");
require([ "a" ], function() {
console.log("c");
});
console.log("TESTING Part Two");
require([ "b" ], function() {
console.log("c");
});

Output 1 - Asynchronous: here is my output with data-dojo-config="async: true"
TESTING Part One
TESTING Part Two
a
c
c
b

Output 2 - Synchronous: here is my output with data-dojo-config=""
TESTING Part One
a
c
TESTING Part Two
b
c

  1. With output 1, why are the "TESTING Part .." strings being output first?
    1. Is it because the two requires in index.html are creating anonymous blocks of code that are being asynchronously?
    2. Is this sneaky way of spinning off threads?
  2. With output 1, why is "b" even being output at all?
    1. From here: http://livedocs.dojotoolkit.org/loader/amd#the-amd-api: "Module creation is lazy and asynchronous, and does not occur immediately when define is called. This means that factory is not executed, and any dependencies of the module will not be resolved, until some running code actually requires the module."
    2. Nothing is actually calling on "b", so why is it executed at all?
  3. With output 2, does this mean that synchronous config turns off this AMD feature: "Module creation is lazy and asynchronous"?