#Retrieve output from multiple runs via `batch.triggerByTaskAndWait`
1 messages · Page 1 of 1 (latest)
You can get the outputs directly from batch.triggerByTaskAndWait without extra API calls.
The function returns an object with a runs array where each item includes ok, output, error, taskIdentifier, etc. You can either index into that array or destructure it:
import { batch, task } from "@trigger.dev/sdk";
export const parentTask = task({
id: "parent-task",
run: async () => {
const { runs } = await batch.triggerByTaskAndWait([
{ task: childTask1, payload: { foo: "World" } },
{ task: childTask2, payload: { bar: 42 } },
]);
if (runs[0].ok) {
// typed as childTask1's return type
console.log("Child task 1 output", runs[0].output);
}
if (runs[1].ok) {
// typed as childTask2's return type
console.log("Child task 2 output", runs[1].output);
}
},
});
export const childTask1 = task({
id: "child-task-1",
run: async (payload: { foo: string }) => {
return `Hello ${payload.foo}`;
},
});
export const childTask2 = task({
id: "child-task-2",
run: async (payload: { bar: number }) => {
return payload.bar + 1;
},
});
You can also use destructuring for clearer typing:
const {
runs: [run1, run2],
} = await batch.triggerByTaskAndWait([
{ task: childTask1, payload: { foo: "World" } },
{ task: childTask2, payload: { bar: 42 } },
]);
if (run1.ok) console.log(run1.output);
if (run2.ok) console.log(run2.output);
This pattern gives you all outputs in one call, so you don’t need additional runs.retrieve requests for a typical batch fan‑out/fan‑in flow. [batch triggerByTaskAndWait]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@balmy dagger Kapa.ai is still learning and improving, please let me know how I did by reacting below.