jrburke/requirejs — pull request #1078 by Demurgos (2014-04-01 16:23:13+00:00)
Title: "delay" handler for definitions with async calls

Some modules do need to perform asynchronous calls to be fully initialized.
In example :
IndexDB calls https://developer.mozilla.org/en/docs/IndexedDB
Ajax requests
Blob builders
Custom methods with asynchronous tasks

To get sure that the asynchronous calls are performed before the defined events are triggered, it is possible to use a plugin that will manually handle those special cases. The problem is that to delay the "define" event, the plugin has to used for each require which creates redundancy across secondary modules depending on the first module requiring async calls. It would be more logical if the async nature of a module was in its definition.
The idea would be to not only load modules asynchronously but also to get their value asynchronously.
The best idea I found was to create a new special handler ( like "require", "exports" or "module" ) called "delay" and being a callback function for the return value.
For example it would look this way :

define(["delay"], function ( delay ) { // depends on the "delay" handler : the value will be returned asynchronously
var Module = ... ;
setTimeout(function(){ //Some asynchronous actions
delay(Module) ; //This does actually returns the Module's value and triggers the "defined" event.
//It's transparent, just as if the loading time over the network was a bit longer.
},1000);
return Module ; //This does not do anything because delay is active
});

This was achieved with only a few lines of code (check the diff) and could offer a useful feature to get a better structured module when its initialization does not simple rely on the load of the file.

PS : I do have a CLA with Dojo