Back to all snippets
Library/JavaScript/Sleep Function
javascriptbeginnerasyncutilitiestiming

How to implement Sleep Function in Javascript

Pause execution for a specified number of milliseconds using async/await

Quick Answer

JavaScript has no built-in sleep, but you can create one by wrapping setTimeout in a Promise and awaiting it inside an async function.

Code Snippet

1const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
2
3// Usage
4async function example() {
5  console.log('Start');
6  await sleep(2000);
7  console.log('After 2 seconds');
8}

What is Sleep Function?

JavaScript does not have a built-in sleep function like other languages, but we can create one using Promises and setTimeout. This allows you to pause execution in async functions, which is useful for rate limiting, testing, or creating delays in animations.

How It Works

  1. 1sleep returns a new Promise that resolves after ms milliseconds.
  2. 2setTimeout calls resolve when the delay expires.
  3. 3await sleep(ms) pauses the async function without blocking the main thread.
  4. 4Execution continues in the next microtask queue cycle after the timeout fires.

Common Use Cases

  • Rate limiting — add delays between API requests
  • Testing — simulate network delays or loading states
  • Animations — create timed sequences
  • Polling — wait between retry attempts

Key Benefits

  • Works seamlessly with async/await
  • Non-blocking execution
  • Simple and readable code
  • Essential for timing control

Common Mistakes to Avoid

  • Calling sleep without await — the Promise is created but not waited on.
  • Using sleep inside non-async functions — you must mark the parent function async.
  • Blocking the main thread by using a busy-wait loop instead of a Promise-based approach.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more javascript examples

Frequently Asked Questions

Does JavaScript sleep block the browser?

No. The Promise-based implementation is non-blocking. The event loop continues to process other tasks while the timeout is pending.

Can I cancel a sleep?

Yes. Wrap setTimeout in a class that exposes a cancel method which calls clearTimeout and resolves the Promise early.

Is there a native sleep in Node.js?

Node 22+ exposes timers/promises with setTimeout that returns a Promise, effectively a built-in sleep.

About This Javascript Code Snippet

This free javascript code snippet for sleep function is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this beginner-level example will help you implement sleep function quickly and correctly.

All snippets in the Snippetly library follow javascript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses javascript.

Tags: async, utilities, timing  | Language: javascript  | Difficulty: beginner  | Category: JavaScript

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.