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.
But it is. One takes a string literal as argument and the other does not.
As mentioned in comment above I also statically analyze.
I agree with the
load
andexecute
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?