Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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