Skip to content

Instantly share code, notes, and snippets.

@ctjlewis
Created January 22, 2020 18:24
Show Gist options
  • Save ctjlewis/931f7c04a359c172afc70df640985c3a to your computer and use it in GitHub Desktop.
Save ctjlewis/931f7c04a359c172afc70df640985c3a to your computer and use it in GitHub Desktop.
Javascript function that waits until a given condition is true. await waitUntil(() => conditionIsTrue(data))
window.waitUntil=function(c){function a(b){c()?b():window.requestAnimationFrame(function(){return a(b)})}return new Promise(a)};
@ctjlewis
Copy link
Author

Un-compiled:

function waitUntil (condition) {
    var poll = (resolve) => {
        if(condition()) resolve();
        else window.requestAnimationFrame(
            () => poll(resolve)
        );
    }

    return new Promise(poll);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment