Skip to content

Instantly share code, notes, and snippets.

@vanrez-nez
Last active January 3, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vanrez-nez/d226b432a6b4ce0f3eb23388b42042b4 to your computer and use it in GitHub Desktop.
Save vanrez-nez/d226b432a6b4ce0f3eb23388b42042b4 to your computer and use it in GitHub Desktop.
Wait for globals to be present
// Micro utility to execute a function when all globals are present
var waitForGlobal = function(params) {
var pathExists = function( arr, obj ) {
var target = obj[ arr.shift() ];
var exists = target !== undefined;
return arr.length == 0 ? exists : exists && pathExists(arr, target);
};
var predicate = function() {
return params.globals.every(function(path) {
return pathExists(path.split('.'), window);
});
};
var wait = function() {
setTimeout( function() {
predicate() && (params.callback() || true) || wait();
}, 30);
};
wait();
};
waitForGlobal({
globals: [ 'google.maps', 'initApp', 'fakeDep' ],
callback: function() {
initApp();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment