Dependency Management Got Awesome
CommonJS and AMD Compliant dependency loader for modern web apps
require.ensure
0.4.x
OLD: This is an old version of documentation. You probably want the most recent version of this
document, from the sidebar on the right.
require.ensure([dependencies], callbackFunction);
The CommonJS specification allows for an asynchronous download method named require.ensure
. It requires an array of dependencies, followed by a callback function. The callback function has the signature
function (require) {}
The only parameter passed to the callback, require
, is a local function that can retrieve any of the dependent modules by the name specified in [dependencies]
.
Ensure Async Dependencies
The primary use of require.ensure
is to demand an unknown number of dependencies, and upon completion, map them back into a local scope.
var deps = ['one', 'two', 'three'];
var modules = {};
require.ensure(deps, function (require) {
for (var i = 0; len = deps.length; i < len; i++) {
modules[deps[i]] = require(deps[i]);
}
// modules now contains the exports of all the dependencies, mapped to their name
});