Skip to main content

Function: when()

when<TInput, TOutput>(config): Processable<TInput, TInput | TOutput>

Defined in: packages/core/src/conditional.ts:215

A two-way branch: run onPass or onFail on the input by a condition.

The condition can be:

  • String: a yes/no question, answered through conditional by the configured decision provider or model
  • Function: returns boolean or Promise
  • Processable<TInput, boolean>

Type Parameters​

TInput​

TInput

TOutput​

TOutput

Parameters​

config​

WhenConfig<TInput, TOutput>

Returns​

Processable<TInput, TInput | TOutput>

Example​

// With function condition
const myPipeline = pipeline(
validatePost,
when({
condition: (post) => post.wordCount > 1000,
onPass: publishImmediately,
onFail: scheduleForReview
})
);

// With string condition (uses AI)
const myPipeline = pipeline(
analyzeContent,
when({
condition: 'this content is high quality and ready to publish',
onPass: publishPost,
onFail: flagForReview
})
);

// With conditional
const isHighQuality = conditional({
goal: 'Determine if content meets quality standards'
});

const myPipeline = pipeline(
processContent,
when({
condition: isHighQuality,
onPass: approve,
onFail: reject
})
);

// onFail is optional - input passes through if condition fails
const myPipeline = pipeline(
loadData,
when({
condition: needsEnrichment,
onPass: enrichData
// No onFail - data passes through unchanged
}),
saveData
);