Last active
July 9, 2020 19:01
-
-
Save schadeck/2ed0c40b25d3ad769c2de5670ab81d12 to your computer and use it in GitHub Desktop.
checkElement #js #promise
This file contains 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
/** | |
* 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