Javascript newbie so I’m guessing this is obvious, but I’m clearly missing something fundamental about RequireJS.
require(['lib/someSlowLoadingLib'], function() {
console.log('Loaded Slow Lib');
console.log(slowLib.incByOne(10)); // shows 11
});
// References slow lib
slowLib.incByOne(10) // throws error, slowLib not found
How can I immediately use what’s being loaded by ‘require’? If it’s loaded asynchronously it’s not available when I need it, which is right away.
I understand that I could put my code inside the require function which would be ok for a single dependency, but if I dependencies are deeply nested this could get messy, no? My main code running within several anonymous functions? If this is the way it’s done that’s fine but it does not seem correct.
You should place all your code that requires someSlowLoadingLib inside the callback where you’re requiring it.
That is the nature of the async pattern.
Anything you place outside of that callback function must not require slowLib.