# Control Flow

## Control flow basics

Adding `if this then that` logic in Laminar can be done using either JavaScript or JQ. Here are examples in both languages:

### JavaScript Example

```javascript
(payload) => {
  const { input } = payload;
  
  if ((input.activity_type === "route-destination-status" || 
       input.activity_type === "update-destinations") && 
      /skipped|failed|completed|loaded/.test(String(input.detailed_event)) &&
      input.order_id?.length > 0) {
    return {
      "lam.resolveThenExecute": {
        "lam.workflowId": 60,
        "lam.payload": input,
        "lam.result": { "status": "OK" }
      }
    };
  } else {
    return { "lam.exit": true };
  }
}
```

### JQ Example

```jq
.input |
if ((.activity_type == "route-destination-status" or
     .activity_type == "update-destinations") and
    (.detailed_event? | tostring | test("skipped|failed|completed|loaded")) and
    (.order_id | length > 0))
then
{
  "lam.resolveThenExecute": {
    "lam.workflowId": 60,
    "lam.payload": .,
    "lam.result": { "status": "OK" }
  }
}
else 
    { "lam.exit": true } 
end
```

## Another Control Flow Example

### JavaScript Version

```javascript
(payload) => {
  const results = payload.step_1.response.results;
  
  if (results.length === 0) {
    return { "lam.exit": true };
  } else if (results.length === 1) {
    return { 
      "order_ids": [Number(results[0].order_id)]
    };
  } else {
    return { "lam.exit": true };
  }
}
```

### JQ Version

```jq
.step_1.response.results | 
if length == 0 then 
  { "lam.exit": true } 
elif length == 1 then 
  .[0] | { "order_ids": [(.order_id | tonumber)] }
else 
  { "lam.exit": true } 
end
```

## Laminar Keywords

Keywords allow workflows to be executed conditionally from within flows, making it easier to support complex control flow logic within custom integrations.
