Skip to content

upstash/workflow-js / skills

upstash-workflow-js

Unverified

Lightweight guidance for using the Upstash Workflow SDK to define, trigger, and manage workflows. Use this Skill whenever a user wants to create workflow endpoints, run steps, or interact with the Upstash Workflow client.

FileKindSize
SKILL.mdskill md2.2 KB
agents.mdmarkdown6.7 KB
basics/client.mdmarkdown4.1 KB
basics/context.mdmarkdown5.1 KB
basics/serve.mdmarkdown2.4 KB
features/flow-control.mdmarkdown3.9 KB
features/invoke.mdmarkdown2.6 KB
features/retries-failures-reliability.mdmarkdown6.3 KB
features/wait-for-event.mdmarkdown2.8 KB
features/webhooks.mdmarkdown2.5 KB
how-to/local-dev.mdmarkdown3.2 KB
how-to/middleware.mdmarkdown4.7 KB
how-to/migrations.mdmarkdown4.8 KB
how-to/realtime.mdmarkdown4.8 KB
rest-api.mdmarkdown6.6 KB
troubleshooting.mdmarkdown4.8 KB

features/wait-for-event.md

View on GitHub
# Wait for Event

Use this feature to pause workflow execution until an external event arrives. It allows building asynchronous, event‑driven workflows without holding compute resources.

---

## How It Works

`context.waitForEvent()` suspends the workflow and registers a waiter under an event ID. When a matching `notify` call is sent, the workflow resumes with the provided event data.

• Each waiter has a timeout. If the event does not arrive in time, the workflow continues with `{ timeout: true }`.
• Maximum timeout depends on your pricing tier.
• Multiple workflow runs may wait on the same event ID; a notify call resumes all of them.

---

## Key Concepts & Pitfalls

### Event IDs

Use unique event IDs to avoid heavy fan‑out notifications.

Example patterns:
• `order-123-processing-complete`
• `user-42-email-verified`

### Race Conditions

A notify call sent _before_ the workflow begins waiting is lost.

To avoid this:
• Always inspect the notify response.
• Retry if no waiters were found.

---

## Combined Example

Below is a single TypeScript example showing waiting for an event, handling timeouts, and safely notifying:

```ts
import { serve } from "@upstash/workflow/nextjs";
import { Client } from "@upstash/workflow";

// Workflow that waits for an event
export const { POST } = serve(async (context) => {
  const { orderId } = context.requestPayload;
  const eventId = `order-${orderId}-processed`;

  // Wait for the event with timeout
  const { eventData, timeout } = await context.waitForEvent(
    "wait-for-order-processing", // step name
    eventId, // event id
    {
      timeout: "1d", // optional timeout
    }
  );

  if (timeout) {
    // handle timeout here
    await context.run("timeout-handler", async () => {
      console.log("Order processing timed out");
    });
    return;
  }

  // Continue workflow using eventData
  await context.run("process-completed-order", async () => {
    console.log("Order processed:", eventData);
  });
});

// External notifier (safe retry pattern)
const client = new Client({ token: "<WORKFLOW_TOKEN>" });

async function notifyProcessingComplete(orderId: string, payload: any) {
  const eventId = `order-${orderId}-processed`;

  // First attempt - returns array of NotifyResponse
  const waiters = await client.notify({ eventId, eventData: payload });

  if (waiters > 0) return waiters;

  // Retry if no workflows were waiting
  await new Promise((r) => setTimeout(r, 3000));
  return await client.notify({ eventId, eventData: payload });
}
```

---

## Best Practices

• Generate event IDs that uniquely identify a specific workflow instance.
• Always handle the timeout case explicitly.
• On notification, check the returned array length to detect if any workflows were waiting.
• Prefer one event ID per workflow run to avoid notifying large groups.
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall