惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
L
LangChain Blog
美团技术团队
N
Netflix TechBlog - Medium
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
博客园 - 司徒正美
爱范儿
爱范儿
D
DataBreaches.Net
月光博客
月光博客
U
Unit 42
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
MongoDB | Blog
MongoDB | Blog
腾讯CDC

Workflow SDK Documentation

Patterns for Defining Tools Human-in-the-Loop Building Durable AI Agents Queueing User Messages Resumable Streams Sleep, Suspense, and Scheduling Streaming Updates from Tools API Reference Workflow Globals Changelog Resilient run start Cookbook Building a World Deploying Astro Express Fastify Hono Getting Started NestJS Next.js Nitro Nuxt Python SvelteKit Vite corrupted-event-log fetch-in-workflow hook-conflict Errors
Timeouts
2026-05-31 · via Workflow SDK Documentation

Add deadlines to slow operations by racing them against a durable sleep.

A common requirement is bounding how long a workflow waits for something to finish — a slow step, an external webhook, a human approval. Race the operation against a durable sleep() with Promise.race() — whichever finishes first wins, and the loser keeps running but its result is ignored.

  • Slow steps — bound the time spent waiting on third-party APIs, model calls, or expensive computation
  • External callbacks — give webhooks a deadline so the workflow doesn't hang forever waiting for an event that may never arrive
  • Human approvals — auto-decline or escalate when a hook isn't resumed within a window
  • Polling loops — give an outer poll-until-ready loop an overall budget

Timeout on a slow step

import { sleep } from "workflow";

declare function processData(data: string): Promise<string>; // @setup

export async function processWithTimeout(data: string) {
  "use workflow";

  const result = await Promise.race([ 
    processData(data), 
    sleep("30s").then(() => "timeout" as const), 
  ]); 

  if (result === "timeout") {
    throw new Error("Processing timed out after 30 seconds");
  }

  return result;
}

Timeout on a webhook

The same pattern works for any promise — including hooks and webhooks. Here a webhook waits for an external service to call back, with a hard deadline of 7 days:

import { sleep, createWebhook } from "workflow";

declare function sendApprovalRequest(requestId: string, webhookUrl: string): Promise<void>; // @setup

export async function waitForApproval(requestId: string) {
  "use workflow";

  const webhook = createWebhook<{ approved: boolean }>();
  await sendApprovalRequest(requestId, webhook.url);

  const result = await Promise.race([ 
    webhook.then((req) => req.json()), 
    sleep("7 days").then(() => ({ timedOut: true }) as const), 
  ]); 

  if ("timedOut" in result) {
    throw new Error("Approval request expired after 7 days");
  }

  // You may see warnings like `Workflow run completed with 1 uncommitted operations` in your
  // logs when the workflow completes. This is expected behavior.

  return result.approved;
}
  1. Durable sleepsleep("30s") persists through restarts at zero compute cost. The workflow resumes precisely when the timer fires.
  2. RacePromise.race([work, sleep(...)]) returns the value of whichever promise resolves first. The loser keeps running in the background but its result is ignored by the workflow.
  3. Discriminated result — tagging the sleep branch with a sentinel value ("timeout" as const, { timedOut: true }) lets TypeScript narrow the result and pick the right branch.
  4. Throw to fail the workflow — inside a workflow function, throwing an Error exits the run with that error. Use FatalError inside steps; throw plain errors inside workflows.

The losing operation keeps running. Promise.race doesn't cancel — when the sleep wins, the underlying step (or model call, or HTTP request) continues to completion in the background. This is fine for idempotent reads but matters when the operation has side effects or costs money. For hard cancellation across processes, see Distributed Abort Controller.

  • Different durationssleep() accepts duration strings ("30s", "5m", "7 days"), milliseconds, or Date objects for absolute deadlines.
  • Soft timeout (retry) — instead of throwing, loop and retry with a fresh Promise.race and a backoff.
  • Soft timeout (fallback) — return a default value when the timer wins instead of throwing: if (result === "timeout") return cachedFallback.
  • Combine with cancellation — race three promises: the operation, a deadline sleep(), and a cancellation hook. See the Scheduling cookbook for the cancellation half of this pattern.
  • Per-step deadlines — wrap each step in its own Promise.race for independent budgets, or use a single outer race for an overall workflow deadline.
  • sleep() — durable wait (survives restarts, zero compute cost)
  • createWebhook() — create a webhook URL the workflow can race against
  • defineHook() — typed hook for in-process cancellation
  • Promise.race() — race operations against deadlines