Skip to content

Commit 2a51a8b

Browse files
committed
document changes to .once()
1 parent d4e96d0 commit 2a51a8b

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

docs/tasks/input-streams.mdx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Type safety is enforced through the generic parameter — both `.send()` and the
108108
| Method | Task suspended? | Compute cost while waiting | Best for |
109109
|--------|----------------|---------------------------|----------|
110110
| `.wait()` | **Yes** | **None** — process freed | Approval gates, human-in-the-loop, long waits |
111-
| `.once()` | No | Full — process stays alive | Short waits, or when doing concurrent work |
111+
| `.once()` | No | Full — process stays alive | Short waits, concurrent work. Returns result object with `.unwrap()` |
112112
| `.on(handler)` | No | Full — process stays alive | Continuous listening (cancel signals, live updates) |
113113

114114
### `wait()` — Suspend until data arrives
@@ -189,6 +189,8 @@ export const processOrder = task({
189189

190190
Blocks until data arrives, but keeps the task process alive. Useful for short waits or when doing concurrent work.
191191

192+
Returns an `InputStreamOncePromise` — similar to `ManualWaitpointPromise` from `.wait()`. Await it for a result object, or chain `.unwrap()` to get the data directly.
193+
192194
```ts
193195
import { task } from "@trigger.dev/sdk";
194196
import { approval } from "./streams";
@@ -198,26 +200,36 @@ export const draftEmailTask = task({
198200
run: async (payload: { to: string; subject: string }) => {
199201
const draft = await generateDraft(payload);
200202

201-
// Task pauses here until someone sends approval
202-
const result = await approval.once();
203+
// Task pauses here until someone sends approval (with a 5-minute timeout)
204+
const result = await approval.once({ timeoutMs: 300_000 });
203205

204-
if (result.approved) {
206+
if (!result.ok) {
207+
// Timed out — result.error is an InputStreamTimeoutError
208+
return { sent: false, timedOut: true };
209+
}
210+
211+
if (result.output.approved) {
205212
await sendEmail(draft);
206-
return { sent: true, reviewer: result.reviewer };
213+
return { sent: true, reviewer: result.output.reviewer };
207214
}
208215

209-
return { sent: false, reviewer: result.reviewer };
216+
return { sent: false, reviewer: result.output.reviewer };
210217
},
211218
});
212219
```
213220

214-
`once()` accepts options for timeouts and abort signals:
221+
Use `.unwrap()` to throw on timeout instead of checking `ok`:
215222

216223
```ts
217-
// With a timeout — rejects if no data arrives within 5 minutes
218-
const result = await approval.once({ timeoutMs: 300_000 });
224+
// Throws InputStreamTimeoutError if no data arrives within 5 minutes
225+
const data = await approval.once({ timeoutMs: 300_000 }).unwrap();
226+
console.log(data.approved); // TData directly
227+
```
219228

220-
// With an abort signal
229+
`once()` also accepts an abort signal for cancellation:
230+
231+
```ts
232+
// With an abort signal — rejects the promise when aborted
221233
const controller = new AbortController();
222234
const result = await approval.once({ signal: controller.signal });
223235
```

0 commit comments

Comments
 (0)