Wait asynchronously in JavaScript

javascript

Updated August 29, 2026

JavaScript has no blocking sleep() that pauses only the current function safely. In asynchronous code, return a promise and await it:

const sleep = (milliseconds) =>
  new Promise(resolve => setTimeout(resolve, milliseconds));

async function main() {
  await sleep(500);
  console.log('continued');
}

main();

await pauses that async function, not the browser's entire main thread. A synchronous busy loop would freeze the page and prevent timers and input from being handled, so do not use it as a sleep substitute.

Timers are minimum delays, not exact schedules. The callback can run later if the event loop is busy or a browser throttles a background tab. For retries, combine a delay with a maximum attempt count, cancellation, and error handling. For animation, use requestAnimationFrame rather than a sleep loop.

Sources

related.

Ruslan Osipov

Ruslan Osipov

About the author