Skip to content

Instantly share code, notes, and snippets.

@KhalilZaidoun
Last active November 29, 2017 13:05
Show Gist options
  • Save KhalilZaidoun/73eef6d0cf4f9f454c32299db7e15b7c to your computer and use it in GitHub Desktop.
Save KhalilZaidoun/73eef6d0cf4f9f454c32299db7e15b7c to your computer and use it in GitHub Desktop.
Function to wait until condition is met before resolving promise
export default function promiseWhen(condition, timeout = 2000) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject();
}, timeout);
const loop = () => {
if (condition()) {
return resolve();
}
setTimeout(loop, 0);
};
setTimeout(loop, 0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment