Skip to content

Instantly share code, notes, and snippets.

@RichardDillman
Created December 30, 2017 09:34
Show Gist options
  • Save RichardDillman/3a04b9cdc6810cfdfb6cb3623107558a to your computer and use it in GitHub Desktop.
Save RichardDillman/3a04b9cdc6810cfdfb6cb3623107558a to your computer and use it in GitHub Desktop.
Checks for an elements existence within a RAF within a promise. Raw
/**
* Checks for an elements existence within a RAF within a promise.
*
* @param {string} selector - The element you wish to find. Defaults to 'body'.
* @param {string} target - The parent element to search within. Defaults to document.
* @return {Promise} Resolves when the element exists.
*/
function elementReady(selector, target) {
var options = {
target: target || document,
selector: selector || 'body'
}
return new Promise(function promise(resolve) {
(function check() {
var el = options.target.querySelector(options.selector);
if (el) {
resolve(el);
}
requestAnimationFrame(check);
})();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment