Skip to main content

Function: recursiveLoop()

recursiveLoop<TInput, TOutput>(config): Processable<TInput, TOutput>

Defined in: packages/core/src/pipeline/iteration.ts:116

Create a recursive loop step that processes nested structures. The body receives a recurse callback to process child elements.

Use this for tree traversal, nested comment processing, or any recursive data structure.

Type Parameters​

TInput​

TInput

TOutput​

TOutput

Parameters​

config​

RecursiveLoopConfig<TInput, TOutput>

Returns​

Processable<TInput, TOutput>

Example​

interface Comment {
id: string;
text: string;
replies?: Comment[];
}

interface ProcessedComment {
id: string;
text: string;
sentiment: string;
replies?: ProcessedComment[];
}

const processComments = pipeline(
loadCommentThread,
recursiveLoop<Comment, ProcessedComment>({
condition: (result, depth) => result.replies && depth < 10,
body: async (comment, recurse) => {
// Process current comment
const sentiment = await analyzeSentiment(comment.text);
const processed = {
id: comment.id,
text: comment.text,
sentiment
};

// Recursively process replies
if (comment.replies) {
processed.replies = await Promise.all(
comment.replies.map(reply => recurse(reply))
);
}

return processed;
},
maxIterations: 100 // Total across all recursive calls
})
);