ExecuteIf
Execute a workflow if a condition is met.
executeIf(condition, workflow)
Execute a workflow if a condition is true.
Arguments
| Argument | Type | Description |
|---|---|---|
| condition | function | Required. Condition needed to execute the workflow. |
| workflow | array | Required. Workflow to execute. |
| elseWorkflow | array | Workflow to execute if the condition fails. |
- The
conditionfunction receive two arguments:containerandrequest. It must return eithertrueorfalse, - The
workflowis simply an array of workers, - The
elseWorkflowis an array of workers to execute if the condition fails.
Examples
In this example, we only want to fetch some data from our API if the weather is sunny:
gateway.route({
method: 'get',
path: '/:username',
workflow: [
aggregate('get', 'https://myapi.com/weather'),
executeIf(({ body }) => body.weather === 'sun', [
aggregate('get', 'https://myapi.com/activities'),
]),
],
});
The response of the request can be, if sunny:
{
"weather": "sun",
"activities": [
"football",
"kayak"
]
}
But, if the weather is not sunny:
{
"weather": "rain"
}