Make sure that whatever you're loading emits an event, that you can hook into.
If you're loading a script file you can hook into the onload
event. If you do an AJAX call you can hook into the onreadystatechange
event.
These are some useful native events.
You can also make more custom events by using Promises.
var modules = {/* */};
var foo = modules.load("foo"); // returns a promise
foo.done(function (foo_module) {
// we now have the foo-module
});
requirejs might be worthwhile to look at, just to see how they do things.
As for promises, this is a good read: What’s so great about JavaScript Promises?