Sleep / Delay Execution
Sleep / Delay Execution is a reusable JavaScript snippet. Pauses async execution for a given number of milliseconds. Useful for rate limiting and timing. The full implementation: // Promise-based sleep const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)) // Usage with async/await async function demo() { console.log('Start') await sleep(2000) console.log('2 seconds later') } // Usage with .then() sleep(1000).then(() => console.log('1 second passed')) This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.