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 therequire("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 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.