JavaScript

Async Sleep / Delay

by @admin
12h ago
Apr 28, 2026
Public
Returns a Promise that resolves after a given number of milliseconds. Use with await to pause async functions without blocking the main thread. More readable than nested setTimeout callbacks and composable with other async utilities.
JavaScript
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

// Usage
async function run() {
  console.log('start');
  await sleep(1000);
  console.log('1 second later');
  await sleep(500);
  console.log('done');
}

run();
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.