sleep

Suspend a workflow for a duration or until a date without consuming resources.

Suspends a workflow for a specified duration or until an end date without consuming any resources. Once the duration or end date passes, the workflow will resume execution.

This is useful when you want to resume a workflow after some duration or date.

sleep is a built-in workflow runtime function and should be called directly inside workflow functions.

import { sleep } from "workflow"

async function testWorkflow() {
    "use workflow"
    await sleep("10s") 
}

API Signature

Parameters

This function has multiple signatures.

Signature 1

NameTypeDescription
durationStringValueThe duration to sleep for, this is a string in the format of "1000ms", "1s", "1m", "1h", or "1d".

Signature 2

NameTypeDescription
dateDateThe date to sleep until, this must be a future date.

Signature 3

NameTypeDescription
durationMsnumberThe duration to sleep for in milliseconds.

Overloads

sleep accepts three types of arguments:

  • StringValue — a human-readable duration string such as "10s", "1m", "1h", or "1d".
  • Date — a future Date object to sleep until.
  • number — a duration in milliseconds.

Examples

Sleeping With a Duration String

You can specify a duration string for sleep to suspend the workflow for a fixed amount of time.

import { sleep } from "workflow"

async function testWorkflow() {
    "use workflow"
    await sleep("1d") 
}

Sleeping With Milliseconds

You can specify a number of milliseconds for sleep to suspend the workflow.

import { sleep } from "workflow"

async function testWorkflow() {
    "use workflow"
    await sleep(10_000) 
}

Sleeping Until an End Date

Pass a Date into the workflow and sleep until that exact timestamp.

import { sleep } from "workflow"

async function testWorkflow(wakeAt: Date) {
    "use workflow"
    await sleep(wakeAt) 
}