Skip to content

Instantly share code, notes, and snippets.

@danielbushman
Last active January 28, 2024 04:40
Show Gist options
  • Save danielbushman/fe81d4e4896135fc99394d139425db12 to your computer and use it in GitHub Desktop.
Save danielbushman/fe81d4e4896135fc99394d139425db12 to your computer and use it in GitHub Desktop.
Timing callbacks adapted to promises
// usage: await timeout(1000);
export const timeout = (timeout = 100) =>
new Promise(resolve => window.setTimeout(resolve, timeout));
// usage: await animationFrame();
export const animationFrame = () =>
new Promise(resolve => window.requestAnimationFrame(resolve));
// usage: await idle();
export const idle = () =>
new Promise(resolve => window.requestIdleCallback(resolve));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment