Skip to content

Instantly share code, notes, and snippets.

@cwatsonc
Last active November 28, 2018 23:15
Show Gist options
  • Save cwatsonc/74a80f164ad2dd7a45af4d5ec6c1e267 to your computer and use it in GitHub Desktop.
Save cwatsonc/74a80f164ad2dd7a45af4d5ec6c1e267 to your computer and use it in GitHub Desktop.
Async Loading using CommonJS require (module.js)
"use strict";
const myObject = require("./my_module.js");
console.log(`myObject is of type: ${typeof myObject}`);
//if myObject has async loaded resources it might not be ready for use
myObject();
"use strict";
asyncLoader = require('asyncloader'); //fictitious module loading a function for illustration
var myMethod; //myMethod is initially undefined
function async myInitializer() {
return await asyncLoader()
}
myInitializer().then((asyncResourceMethod) => {
myMethod = exports = asyncResourceMethod; //when this code is executed then exports has a value
})
}
//believe that exports is exposed here as 'undefined'
@cwatsonc
Copy link
Author

Want to have clients use the CommonJS method of loading and to ensure that the function returned is ready for use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment