Skip to content

Instantly share code, notes, and snippets.

@kriskowal
Created December 9, 2011 18:43
Show Gist options
  • 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.
@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