Skip to main content
You can pause a workflow run with the waitForEvent step. An event is uniquely identified by event ID. The workflow will resume when the matching event is published. waitForEvent supports configurable timeouts to prevent workflows from waiting indefinitely. When a timeout occurs, the returned object includes timeout: true, allowing you to handle the failure case gracefully (for example, cancel an order, notify the user, or retry later).
If no timeout is specified, the default is 7 days.
import { serve } from "@upstash/workflow/nextjs";

export const { POST } = serve<string>(async (context) => {
  const { orderId, userEmail } = context.requestPayload;

  // Wait for order processing completion
  const { eventData, timeout } = await context.waitForEvent(
    "wait-for-order-processing",
    `order-${orderId}`,
    {
      timeout: "1d" // 1 day timeout
    }
  );

  if (timeout) {
    // Handle timeout scenario
    await context.run("handle-timeout", async () => {
      return await handleOrderTimeout(orderId, userEmail);
    });
    return;
  }

});