Skip to content

Instantly share code, notes, and snippets.

@westc
Created June 2, 2026 23:30
Show Gist options
  • Select an option

  • Save westc/3ce70ca77d48add157983bcb5bf5bc7d to your computer and use it in GitHub Desktop.

Select an option

Save westc/3ce70ca77d48add157983bcb5bf5bc7d to your computer and use it in GitHub Desktop.
A simple way to poll for something in JS.
/**
* @typedef poll__options
* @property {((criteriaResult: any) => void)=} callback
* Optional. If not specified then `poll` returns a promise that will resolve once `criteria` returns a `true`-ish value.
* @property {number} [timeout=50]
* Optional. Number of milliseconds between one call to criteria and the next (excluding the initial which is immediate). Defaults to `50`.
* @property {boolean} [ignoreErrors=false]
* Optional. Whether or not an error coming from `criteria` will be ignored. Defaults to `false`.
*/
/**
* @param {() => any} criteria
* Function that is run every timeout period to determine whether the polling can stop.
* @param {poll__options=} options
*/
function poll(criteria, options) {
const {callback, timeout, ignoreErrors} = options ?? {};
function attempt(resolve) {
let result;
try {
result = criteria();
}
catch (err) {
if (!ignoreErrors) throw err;
}
if (result) {
resolve(result);
}
else {
setTimeout(() => attempt(resolve), timeout ?? 50);
}
}
if (callback) {
attempt(callback);
}
else {
return new Promise(resolve => void attempt(resolve));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment