Skip to content

Instantly share code, notes, and snippets.

@seonglae
Created June 25, 2021 16:35
Show Gist options
  • Select an option

  • Save seonglae/e14a8e013e5a10522f1caa10eaf89a05 to your computer and use it in GitHub Desktop.

Select an option

Save seonglae/e14a8e013e5a10522f1caa10eaf89a05 to your computer and use it in GitHub Desktop.
Wait Until Verifiyer Function return true Interval Promise Function
async function waitUntil<T>(
flag: (...args: any[]) => boolean,
callback: (...args: any[]) => T,
args: any[] = [],
time: number = 50
): Promise<T> {
return new Promise(async resolve =>
flag() ? resolve(await callback(...args)) : setTimeout(() => this.waitUntil(flag, callback, args, time), time)
)
}
@seonglae
Copy link
Author

seonglae commented Jun 30, 2021

Javascript Version

async function waitAvail(flag, callback, args, time = 500) {
    /*
     * @summary - Wait until flag function return true
     */
    return new Promise(async resolve =>
      flag() ? resolve(await callback(...args)) : setTimeout(() => waitAvail(flag, callback, args, time), time)
    )
  }

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