Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created December 9, 2011 18:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriskowal/1452767 to your computer and use it in GitHub Desktop.
Save kriskowal/1452767 to your computer and use it in GitHub Desktop.
(Un)CommonJS Asynchronous Module Proposal Differences

In both cases below, ID is the exports object of the "id" module.

require.ensure(["id"], function (require) {
    var ID = require("id");
})
  • In this example, the "id" module does not need to be loaded before executing this module. Because it uses the require("id") syntax, it is not simple to distinguish it from a dependency that must be preloaded.
require.async("id", function (ID) {
}, function (error) {
});
  • does not accept multiple identifiers. The presumption is that it will be more common and usable for the loaded module to statically depend on anything you would be tempted to import in the same async call. If this presumption does not pan out (in my own use, it has), it is easy enough to extend this specification with spread/rest (variadic) arguments.
@cadorn
Copy link

cadorn commented Dec 15, 2011

the inner require is not easily distinguishable from an outer

But it is. One takes a string literal as argument and the other does not.

UncommonJS/Modules are still firmly in the world of statically analyzing require() calls instead of declaring them up front.

As mentioned in comment above I also statically analyze.

I agree with the load and execute distinction. The only case where that should matter though is where a module acts on the global environment (which you may want to happen at a precise time) vs its own exports only. Is that a fair assumption? Under what other conditions would you want to load and not execute right away?

@kriskowal
Copy link
Author

@cadorn I see, regarding statically analyzable. I still do not think I could sell someone on lacing it through when it’s simpler to just provide the exports. If someone wants to load and execute lazily, the spec can grow a require.load(id, cb, eb) just as well. I can’t think of a case where I would lazily load and not immediately execute. Global patching really should happen outside the purview of the module system, or at least before anything else is executed. There are a lot of ways to guarantee that.

@cadorn
Copy link

cadorn commented Dec 15, 2011

Global patching really should happen outside the purview of the module system

Right. Exactly my point and hence no real need to have the load/execute separate.

So the UncommonJS/Modules variant works for me as long as I can get the canonical id of "id" in the success callback but that can happen via a separate look up method so no problem there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment