Skip to content

Instantly share code, notes, and snippets.

@rdillmanCN
Created December 30, 2017 03:35
Show Gist options
  • Save rdillmanCN/4eadb4e7738088b7f2d9a60407e0366a to your computer and use it in GitHub Desktop.
Save rdillmanCN/4eadb4e7738088b7f2d9a60407e0366a to your computer and use it in GitHub Desktop.
Checks for an elements existence within a RAF within a promise.
/**
* 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