dojo/when — The Dojo Toolkit (original) (raw)

In this example, we demonstrate how we can handle both a synchronous and asynchronous process with the same code, by using when.

require(["dojo/when", "dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"], function(when, Deferred, dom, on){ function asyncProcess(){ var deferred = new Deferred();

setTimeout(function(){
  deferred.resolve("async");
}, 1000);

return deferred.promise;

}

function syncProcess(){ return "sync"; }

function outputValue(value){ dom.byId("output").innerHTML += "
completed with value: " + value; }

on(dom.byId("startButton"), "click", function(){ when(asyncProcess(), outputValue); when(syncProcess(), outputValue); });

});

Output:

Start