Last active
November 28, 2018 23:15
-
-
Save cwatsonc/74a80f164ad2dd7a45af4d5ec6c1e267 to your computer and use it in GitHub Desktop.
Async Loading using CommonJS require (module.js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Want to have clients use the CommonJS method of loading and to ensure that the function returned is ready for use.