Delayed Functions

You can easily run functions in the future with Inngest. There are three ways to delay function execution:

Delays can be up to a year (up to seven days on the free plan). There are some benefits to delaying functions using Inngest:

  • It works across any provider or platform
  • Delays are durable and work across server restarts, serverless functions, and redeploys
  • You can schedule functions into the far future
  • Serverless functions are fully supported on all platforms
  • Our SDK bypasses serverless function timeouts on all platforms
  • You never need to manage queues or backlogs

Platform support

This works across all providers and platforms, whether you run serverless functions or use servers like express. It also bypasses serverless function timeouts on all platforms, so you can sleep for a longer time than your provider supports.

Sleep for a duration

You can pause a function for a set amount of time using the step.sleep() method:

import { Inngest } from "inngest";

const inngest = new Inngest({ id: "signup-flow" });

export const fn = inngest.createFunction(
  { id: "send-signup-email" },
  { event: "app/user.created" },
  async ({ event, step }) => {
    await step.sleep("wait-a-moment", "1 hour");
    await step.run("do-some-work-in-the-future", async () => {
      // This runs after 1 hour
    });
  }
);

For more information on step.sleep() read the reference.

Sleep until a specific time

You can pause a function until a specific time using the step.sleepUntil() method:

import { Inngest } from "inngest";

const inngest = new Inngest({ id: "signup-flow" });

export const fn = inngest.createFunction(
  { id: "send-signup-email" },
  { event: "app/user.created" },
  async ({ event, step }) => {
    await step.sleepUntil("wait-for-iso-string", "2023-04-01T12:30:00");

    // You can also sleep until a timestamp within the event data.  This lets you
    // pass in a time for you to run the job:
    await step.sleepUntil("wait-for-timestamp", event.data.run_at); // Assuming event.data.run_at is a timestamp.

    await step.run("do-some-work-in-the-future", async () => {
      // This runs at the specified time.
    });
  }
);

For more information on step.sleepUntil() read the reference.

Schedule a function for later

You can also delay when a function starts by setting the ts field in the event payload to a future timestamp (milliseconds since the Unix epoch). Unlike step.sleep() or step.sleepUntil() which add delays within a function, the ts field delays the start of the function run itself.

For example, to schedule a function to run 5 minutes from now:

await inngest.send({
  name: "notifications/reminder.scheduled",
  data: {
    user: { email: "johnny.utah@fbi.gov" },
    message: "Don't forget to catch the wave at 3pm",
  },
  // Include the timestamp for 5 minutes in the future:
  ts: Date.now() + 5 * 60 * 1000,
});

For a complete walkthrough, see the Scheduling a one-off function example.

How it works

step.sleep() and step.sleepUntil()

With step.sleep() and step.sleepUntil(), the function controls when it runs. You control the flow of your code by calling sleep or sleepUntil within your function directly, instead of using the queue to manage your code's timing. This keeps your logic together and makes your code easier to modify.

Inngest stops the function from running for whatever time is specified. When you call step.sleep or step.sleepUntil the function automatically stops running any future work. The function then tells the Inngest executor that it should be re-invoked at a future time. We re-call the function at the next step, skipping any previous work. This is how we bypass serverless function time limits and work across server restarts or redeploys.

Event ts field

When you set the ts field on an event to a future Unix timestamp, Inngest delays invoking the function until that time. The function itself runs normally — the delay happens before it starts, not within it. This is useful when the delay is known at the time the event is sent and you don't need any logic to run before the wait.