Skip to content

Instantly share code, notes, and snippets.

Created June 1, 2018 01:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
wait until condition becomes truthy. Inspired by former works or wait-until libraries and tutorials.
const waitUntil = async (asyncCondition, timeoutMs) => {
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
const intervalMs = 1000;
const startTime =;
const result = await asyncCondition();
const endTime =;
const timeSpentMs = endTime - startTime;
console.log(`checking waitUntil condition, timeout in: ${timeoutMs}ms`);
if (result) return result;
if (timeoutMs - timeSpentMs - intervalMs < 0) return result;
await wait(intervalMs);
return await waitUntil(asyncCondition, timeoutMs - timeSpentMs - intervalMs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment