Skip to content

Commit 715d1b9

Browse files
committed
advice in docs re parallel dependency waits
1 parent 8f434cb commit 715d1b9

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

docs/v3/triggering.mdx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,43 @@ export const myTask = task({
161161

162162
This is where it gets interesting. You can trigger a task and then wait for the result. This is useful when you need to call a different task and then use the result to continue with your task.
163163

164+
<Accordion title="Don't use this in parallel, e.g. with `Promise.all()`">
165+
Instead, use `batchTriggerAndWait()` if you can, or a for loop if you can't.
166+
167+
<CodeGroup>
168+
```ts /trigger/batch.ts
169+
export const batchTask = task({
170+
id: "batch-task",
171+
run: async (payload: string) => {
172+
const results = await childTask.batchTriggerAndWait({
173+
items: [{ payload: "item1" }, { payload: "item2" }],
174+
// optional: how many to run in parallel
175+
batchOptions: { maxConcurrency: 1 },
176+
});
177+
console.log("Results", results);
178+
179+
//...do stuff with the results
180+
},
181+
});
182+
```
183+
184+
```ts /trigger/loop.ts
185+
export const loopTask = task({
186+
id: "loop-task",
187+
run: async (payload: string) => {
188+
for (let i = 0; i < 2; i++) {
189+
const result = await childTask.triggerAndWait({ payload: `item${i}` });
190+
console.log("Result", result);
191+
192+
//...do stuff with the result
193+
}
194+
},
195+
});
196+
```
197+
</CodeGroup>
198+
199+
</Accordion>
200+
164201
```ts /trigger/parent.ts
165202
export const parentTask = task({
166203
id: "parent-task",
@@ -177,6 +214,47 @@ export const parentTask = task({
177214

178215
You can batch trigger a task and wait for all the results. This is useful for the fan-out pattern, where you need to call a task multiple times and then wait for all the results to continue with your task.
179216

217+
<Accordion title="Don't use this in parallel, e.g. with `Promise.all()`">
218+
Instead, pass in all items at once and set an appropriate `maxConcurrency`. Alternatively, use sequentially with a for loop.
219+
220+
<CodeGroup>
221+
```ts /trigger/batch.ts
222+
export const batchTask = task({
223+
id: "batch-task",
224+
run: async (payload: string) => {
225+
const results = await childTask.batchTriggerAndWait({
226+
items: [{ payload: "item1" }, { payload: "item2" }],
227+
// optional: how many tasks to run in parallel
228+
batchOptions: { maxConcurrency: 1 },
229+
});
230+
console.log("Results", results);
231+
232+
//...do stuff with the results
233+
},
234+
});
235+
```
236+
237+
```ts /trigger/loop.ts
238+
export const loopTask = task({
239+
id: "loop-task",
240+
run: async (payload: string) => {
241+
for (let i = 0; i < 2; i++) {
242+
const result = await childTask.batchTriggerAndWait({
243+
items: [{ payload: `itemA${i}` }, { payload: `itemB${i}` }],
244+
// optional: how many tasks to run in parallel
245+
batchOptions: { maxConcurrency: 1 },
246+
});
247+
console.log("Result", result);
248+
249+
//...do stuff with the result
250+
}
251+
},
252+
});
253+
```
254+
</CodeGroup>
255+
256+
</Accordion>
257+
180258
```ts /trigger/nested.ts
181259
export const batchParentTask = task({
182260
id: "parent-task",

0 commit comments

Comments
 (0)