Last active
January 3, 2018 18:19
-
-
Save vanrez-nez/d226b432a6b4ce0f3eb23388b42042b4 to your computer and use it in GitHub Desktop.
Wait for globals to be present
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
// 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