Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save phillipharding/9f5d3c175ab7e45e062e to your computer and use it in GitHub Desktop.
Save phillipharding/9f5d3c175ab7e45e062e to your computer and use it in GitHub Desktop.
Load and Synchronise Execution of Multiple Scripts using Promises
/* load and synchronise execution of 2 or more scripts */
var
upsuModulesLoaded = [
RB.Masterpage.LoadResourceFromTenantRoot("_catalogs/masterpage/Buzz365/js/siteusage.js"),
RB.Masterpage.LoadResourceFromTenantRoot("_catalogs/masterpage/Buzz365/js/userprofile.js")
];
$.when.apply($, upsuModulesLoaded)
.done(function() {
RB.Masterpage.Log("Masterpage>>userprofile.js and siteusage.js have loaded");
var waitCount = 0;
function deferAndWaitForModuleExecution() {
/* this is an edge case:
while the scripts have been loaded they '**may**' not have executed yet, so we
may have to wait for this to happen before we can call the module init functions.
*/
if (!RB.Masterpage.IsValidType('RB.Masterpage.Siteusage.EnsureSetup') || !RB.Masterpage.IsValidType('RB.Masterpage.Userprofile.EnsureSetup')) {
waitCount++;
if (waitCount > 10)
RB.Masterpage.UpSuModulesInitialised.reject(); /* give up after waiting for execution for 2 seconds */
else {
RB.Masterpage.Log("Masterpage>>deferAndWaitForModuleExecution("+waitCount+") for USERPROFILE.JS and SITEUSAGE.JS initialisation");
setTimeout(deferAndWaitForModuleExecution, 200);
}
return;
}
var moduleInits = [
RB.Masterpage.Siteusage.EnsureSetup(),
RB.Masterpage.Userprofile.EnsureSetup()
];
$.when.apply($, moduleInits)
.done(function() {
RB.Masterpage.UpSuModulesInitialised.resolve();
});
}
deferAndWaitForModuleExecution();
});
RB.Masterpage.UpSuModulesInitialised
.done(function() {
RB.Masterpage.Log("Masterpage>>userprofile.js and siteusage.js have initialised");
RB.Masterpage.Siteusage.Acceptance();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment