> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/vercel/workflow/llms.txt
> Use this file to discover all available pages before exploring further.

# sleep

> Pause workflow execution for a duration or until a specific time

Sleep within a workflow for a given duration or until a specific date. This is a durable sleep that persists across workflow suspensions and resumptions.

## Signature

```typescript theme={null}
function sleep(duration: StringValue): Promise<void>
function sleep(date: Date): Promise<void>
function sleep(durationMs: number): Promise<void>
```

## Parameters

<ParamField path="duration" type="StringValue">
  A duration string in the format of `"1000ms"`, `"1s"`, `"1m"`, `"1h"`, or `"1d"`.
</ParamField>

<ParamField path="date" type="Date">
  A Date object representing when to resume. Must be a future date.
</ParamField>

<ParamField path="durationMs" type="number">
  The duration to sleep for in milliseconds.
</ParamField>

## Returns

<ResponseField name="Promise<void>" type="Promise<void>">
  A promise that resolves when the sleep duration has elapsed.
</ResponseField>

## Usage

### Sleep with Duration String

Pause for a human-readable duration:

```typescript theme={null}
import { sleep } from 'workflow';

export async function workflowWithSleep() {
  "use workflow";

  console.log('Starting...');
  
  await sleep('5s');
  console.log('After 5 seconds');
  
  await sleep('2m');
  console.log('After 2 minutes');
  
  await sleep('1h');
  console.log('After 1 hour');
  
  await sleep('1d');
  console.log('After 1 day');
}
```

### Sleep with Milliseconds

Pause for a specific number of milliseconds:

```typescript theme={null}
import { sleep } from 'workflow';

export async function workflowWithMsSleep() {
  "use workflow";

  console.log('Starting...');
  
  await sleep(5000); // 5 seconds
  console.log('After 5000ms');
  
  await sleep(60000); // 1 minute
  console.log('After 60000ms');
}
```

### Sleep Until Specific Date

Pause until a specific point in time:

```typescript theme={null}
import { sleep } from 'workflow';

export async function workflowWithDateSleep() {
  "use workflow";

  const tomorrow = new Date();
  tomorrow.setDate(tomorrow.getDate() + 1);
  tomorrow.setHours(9, 0, 0, 0); // 9 AM tomorrow
  
  console.log('Sleeping until:', tomorrow);
  await sleep(tomorrow);
  console.log('Good morning!');
}
```

### Retry Pattern

Implement exponential backoff:

```typescript theme={null}
import { sleep } from 'workflow';

export async function workflowWithRetry() {
  "use workflow";

  let attempt = 0;
  const maxAttempts = 5;
  
  while (attempt < maxAttempts) {
    try {
      await processData();
      break;
    } catch (error) {
      attempt++;
      if (attempt < maxAttempts) {
        const delay = Math.pow(2, attempt) * 1000; // Exponential backoff
        console.log(`Attempt ${attempt} failed, retrying in ${delay}ms`);
        await sleep(delay);
      }
    }
  }
}
```

### Schedule Future Task

Schedule work for a specific time:

```typescript theme={null}
import { sleep } from 'workflow';

export async function scheduledWorkflow(scheduledTime: Date) {
  "use workflow";

  console.log('Waiting for scheduled time:', scheduledTime);
  await sleep(scheduledTime);
  
  console.log('Executing scheduled task');
  await executeTask();
}
```

### Polling with Delays

Poll an external system with delays:

```typescript theme={null}
import { sleep } from 'workflow';

export async function pollingWorkflow() {
  "use workflow";

  let status = 'pending';
  
  while (status === 'pending') {
    status = await checkStatus();
    console.log('Status:', status);
    
    if (status === 'pending') {
      await sleep('30s'); // Poll every 30 seconds
    }
  }
  
  console.log('Final status:', status);
}
```

### Rate Limiting

Control the rate of operations:

```typescript theme={null}
import { sleep } from 'workflow';

export async function rateLimitedWorkflow(items: string[]) {
  "use workflow";

  for (const item of items) {
    await processItem(item);
    await sleep('1s'); // Rate limit to 1 item per second
  }
}
```

## Duration Format

The sleep function accepts duration strings in the following formats:

* `"1000ms"` - Milliseconds
* `"1s"` - Seconds
* `"1m"` - Minutes
* `"1h"` - Hours
* `"1d"` - Days

You can also combine units: `"1h 30m"`, `"2d 3h"`, etc.

## Notes

* Can only be called inside a workflow function (with `"use workflow"`)
* This is a durable sleep that persists across workflow suspensions
* The workflow will be suspended during sleep and resumed when the duration elapses
* Sleep uses timer events in the event log for durability
* When sleeping until a date, the date must be in the future
* Very long sleep durations (months/years) are supported

## Related

* [RetryableError](/api-reference/workflow/retryable-error) - Control step retry timing


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.