Skip to content

Instantly share code, notes, and snippets.

@pke
Created April 10, 2014 23:33
Show Gist options
  • Save pke/10432011 to your computer and use it in GitHub Desktop.
Save pke/10432011 to your computer and use it in GitHub Desktop.
Async version of "require" for requireJS in WinJS Apps
window.requireAsync = function(deps) {
if (arguments.length === 0) {
throw new TypeError("requireAsync: No dependencies given");
}
if (typeof deps === "string" && arguments.length === 1) {
deps = deps.split(",");
} else if (!Array.isArray(deps)) {
deps = Array.prototype.slice.call(arguments, 0);
}
return new WinJS.Promise(function(c, e, p) {
return require(deps, function() {
return c.call(this, Array.prototype.slice.call(arguments, 0));
}, e);
});
};
// Call it like this
requireAsync("module").then(function(imports){
var module = imports[0];
});
requireAsync("module1,module2").then(function(imports){
var module1 = imports[0],
module2 = imports[1];
});
requireAsync("module1", "module2").then(function(imports){
var module1 = imports[0],
module2 = imports[1];
});
requireAsync(["module1","module2"]).then(function(imports){
var module1 = imports[0],
module2 = imports[1];
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment