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.
@kriskowal Your assumption about statically linked dependencies for the async loaded module holds true for me too. So far I have not needed to load more than one module at a time.
Why
require.async()
vsmodule.async()
ormodule.load()
? CommonJS/Modules/2 (draft) suggestsmodule.load()
for reasons I don't 100% remember but there is a thread about it.I am currently using:
Where
mappedID
is a package local ID that will resolve first term against package mappings or it can be a relative ID. HavingcanonicalID
ID has been useful. Exports are not always needed right away. I have the need for the error callback as well so will likely add that as third argument.