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

# Sleep

> Pause a run for minutes, days, or months, with nothing running in your app while it waits.

A run can stop for a set time, then carry on: a reminder three days after sign-up, a follow-up a week after an order, a retry an hour later. While it sleeps, your app has nothing running and nothing in memory. Nelkap keeps the run, and calls your app again when the time is up.

```ts theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
export async function sendTrialReminder(userId: string) {
  await nelkap.step.sleep('11 days');

  await nelkap.step.run('reminder', () =>
    sendTrialEndingEmail(userId),
  );
}
```

Started when a trial begins, this run sleeps for 11 days, then sends the reminder.

## Durations

A duration is a count and a unit: `30 seconds`, `15 minutes`, `2 hours`, `3 days`, `1 week`, `6 months`, `1 year`. The unit can be singular or plural, and a sleep lasts up to a year.

Seconds, minutes, hours, days, and weeks are fixed lengths of time. Months and years follow the calendar: `1 month` from January 15 ends on February 15. A month from January 31 runs past the end of February and ends in early March.

## Wake-up time

Nelkap wakes sleeping runs every minute, so a run goes on within a minute after its time is up. A sleep of a few seconds therefore lasts up to a minute. For a pause shorter than that, wait in your code inside a step instead.

When the run wakes, Nelkap calls your app, and your workflow runs from the top: the steps before the sleep give back their saved results, the sleep is over, and the work after it runs.

## Sleep until a date

`step.sleep` takes a duration. To sleep until a date, work out the duration in a step, so every call of the run gets the same one:

```ts theme={"theme":{"light":"light-plus","dark":"dark-plus"}}
export async function remindBeforeEvent(event: {
  id: string;
  startsAt: string;
}) {
  const hours = await nelkap.step.run(
    'hours left',
    () => {
      const hoursUntilStart =
        (Date.parse(event.startsAt) - Date.now()) /
        3_600_000;

      return Math.max(
        0,
        Math.floor(hoursUntilStart) - 24,
      );
    },
  );

  await nelkap.step.sleep(`${hours} hours`);

  await nelkap.step.run('reminder', () =>
    sendEventReminder(event.id),
  );
}
```

This run sends its reminder a day before the event. The number of hours is saved, so a later call doesn't compute a new one from a later time. If the event is less than a day away, the duration is `0 hours`, and a sleep of zero goes on at once.

## Sleep names

A sleep is saved like a step, named after its duration. When a run reaches two sleeps of `1 day`, the first matches the first saved one, and the second matches the second. So changing a sleep's duration in your code makes it a new sleep for the runs that already passed it. See [Versioning](https://docs.nelkap.com/guides/versioning.md).

## Cancel a sleeping run

While a run sleeps, you can cancel it from its page, for example when the user upgrades before the trial reminder goes out. See [Runs](https://docs.nelkap.com/monitor/runs.md#cancel-a-run).


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