Opened 13 years ago

Closed 13 years ago

Last modified 8 years ago

#4096 closed enhancement (wontfix)

dojo.require should return a Deferred

Reported by: dante Owned by: alex
Priority: high Milestone:
Component: Loader Version: 0.9
Keywords: Cc: James Burke
Blocked By: Blocking:


not fully certain the details of this, but alex can chime in: but it's essentially a way to know if a specific module has been loaded:

dojo.require("dijit.form.Button").addCallback(function(){ /* do something */ }); 

Change History (4)

comment:1 Changed 13 years ago by James Burke

I'd like to know the use case for needing this. We have dojo.addOnLoad() which does effectively the same thing. It does not fire for each require, but for the full batch of requests. dojo.addOnLoad() even works after the page load. In practice, waiting for all resources that were dojo.require'd before running dependent code seems to be the 95% use case (the dojo.addOnLoad() case)?

I'm also concerned about API confusion: we're giving another thing the user has to consider when loading code. I'm not sure if this fits with constraining the user's choices to make things easier.

Handling errors gets more complicated too, given that Deferred likes to catch exceptions.

Seems like more code in both the xd and non-xd base, and slower code, since we have to new up Deferreds for every require call that is made (both at the top level and for every require in every dependent file). This seems particularly wasteful in a custom build.

I also do not like having to depend on Deferred in the loader. This is a lesser concern, but I like the idea of being able to build "just the Dojo loader" with no dependencies on the base modules. It would be nice to give "just the loader" for Dojo for use in other people's code. May not be realistic at this point, so less of concern, but I like having the least number of dependencies in the loader on other Dojo code.

comment:2 Changed 13 years ago by Adam Peller

Milestone: 1.0

sounds like this might be a wontfix?

comment:3 Changed 13 years ago by dante

Resolution: wontfix
Status: newclosed

i'm inclined to agree with you after this time.

comment:4 Changed 8 years ago by bishop

Use case: a factory method that returns a promise. In some cases, all resources are available and the factory returns a deferred that is already resolved. In other cases, some external resources must be required before manufacture. The pattern would be:

factory:function (driver) {
  switch (driver.toLowerCase()) {
  case 'local':
    var resource = /* build some local resource */
    return new Deferred().resolve(resource);

  case 'remote':
    return require([''] function (Foo) {
        var resource = new Foo() /* whatever to build the resource from the load */
        return resource;

The promise is the require return result, the call back is the resolution. Absent this requested behavior, extra hoops are needed.

On edit: By "extra hoops", I mean when():

Last edited 8 years ago by bishop (previous) (diff)
Note: See TracTickets for help on using tickets.