Skip to content

Instantly share code, notes, and snippets.

@linktohack
Created July 21, 2015 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linktohack/b1ffc5b494d430461b44 to your computer and use it in GitHub Desktop.
Save linktohack/b1ffc5b494d430461b44 to your computer and use it in GitHub Desktop.
waitFor as a Promise
function waitFor(testFx, onReady, timeOutMillis) {
return new Promise(function (resolve, reject) {
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000,
start = new Date().getTime(),
condition = false,
interval = setInterval(function() {
if ((new Date().getTime() - start < maxtimeOutMillis) && !condition) {
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx());
} else {
if(!condition) {
reject(new Error("'waitFor()' timeout"));
} else {
resolve(typeof(onReady) === "string" ?eval(onReady) :onReady());
clearInterval(interval);
}
}
}, 250);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment