Skip to content

Instantly share code, notes, and snippets.

@schadeck
Last active July 9, 2020 19:01
Show Gist options
  • Save schadeck/2ed0c40b25d3ad769c2de5670ab81d12 to your computer and use it in GitHub Desktop.
Save schadeck/2ed0c40b25d3ad769c2de5670ab81d12 to your computer and use it in GitHub Desktop.
checkElement #js #promise
/**
* rafAsync
* using requestAnimationFrame to recheck for element
*/
const rafAsync = () => {
return new Promise(resolve => {
requestAnimationFrame(resolve);
});
}
/**
* checkElement
* checking DOM for element until it exists then returning a promise
* @param {selector} selector
*/
export const checkElement = selector => {
if (document.querySelector(selector) === null) {
return rafAsync().then(() => checkElement(selector));
} else {
return Promise.resolve(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment