RAP Logo

while-loop

Help flow looping if the condition is true for conditional iterative processing.

while-loop Block

The while-loop block helps flow looping if the condition is true.

Overview

The while-loop block executes conditional loops that continue until a condition is met. It enables complex iterative processing where the number of iterations is not known in advance, making it perfect for dynamic processing scenarios.

Configuration Options

Loop Condition

  • Condition: JavaScript expression that determines whether to continue looping
  • Condition Type: Type of condition evaluation (expression, property comparison, etc.)
  • Initial Condition: The initial state of the loop condition
  • Condition Update: How to update the condition for the next iteration

Loop Behavior

  • Loop Direction: Forward or backward iteration
  • Maximum Iterations: Safety limit to prevent infinite loops
  • Condition Check: When to evaluate the condition (before or after processing)
  • Output Behavior: What to send on each iteration and when complete

How It Works

The while-loop block:

  1. Evaluates Condition: Checks if the loop condition is true
  2. Executes Loop: If condition is true, processes the current iteration
  3. Updates State: Modifies the condition or state for the next iteration
  4. Repeats: Continues until the condition becomes false
  5. Completes: Sends a completion message when the loop finishes

Basic While Loop Example

// Configuration
Condition: msg.payload.count < 10
Maximum Iterations: 100

// Initial message
{ payload: { count: 0 } }

// Loop iterations
Iteration 1: count = 0, condition = true → process → count = 1
Iteration 2: count = 1, condition = true → process → count = 2
...
Iteration 10: count = 10, condition = false → exit loop

Use Cases

Dynamic Processing

Process data until a condition is met:

input → while-loop (condition) → process → update condition → next iteration

Retry Logic

Retry operations until successful:

while-loop (retry condition) → attempt operation → check result → retry or exit

Data Validation

Validate data until it meets criteria:

while-loop (validation condition) → validate → fix issues → revalidate

State Machine

Implement state machine logic:

while-loop (state condition) → process state → transition state → next state

Common Patterns

Counter-Based Loop

// Configuration
Condition: msg.payload.count < msg.payload.maxCount
Maximum Iterations: 1000

// Process with counter
while-loop → increment counter → process → check condition → next iteration

Status-Based Loop

// Configuration
Condition: msg.payload.status !== "complete"
Maximum Iterations: 50

// Process until complete
while-loop → check status → process → update status → next iteration

Error-Based Loop

// Configuration
Condition: msg.payload.errorCount < 3
Maximum Iterations: 10

// Retry on errors
while-loop → attempt operation → check for errors → retry or exit

Advanced Features

Complex Conditions

Use sophisticated condition expressions:

// Multiple conditions
Condition: msg.payload.count < 100 && msg.payload.errors < 5;

// Property-based conditions
Condition: msg.payload.status === "processing" && msg.payload.retries < 3;

// Context-based conditions
Condition: flow.get("processing") === true &&
  global.get("systemReady") === true;

Dynamic Condition Updates

Modify conditions during loop execution:

// Update condition based on processing results
if (msg.payload.result === "success") {
  flow.set("continueLoop", false);
} else {
  flow.set("retryCount", flow.get("retryCount") + 1);
}

Nested Loops

Combine with other loop types:

// Outer while loop
while-loop (outer condition) →
    // Inner counter loop
    counter-loop (inner count) →
        process →
    next inner iteration →
next outer iteration

Tips

  • Set Maximum Iterations: Always set a safety limit to prevent infinite loops
  • Use Clear Conditions: Write conditions that are easy to understand and debug
  • Handle Edge Cases: Consider what happens when conditions are never met
  • Monitor Performance: Be mindful of loop performance and resource usage
  • Test Thoroughly: Verify behavior under various condition scenarios
  • Use Meaningful Variables: Store loop state in descriptive message properties

Common Issues

Infinite Loops

Avoid conditions that never become false:

// BAD: This will loop forever
Condition: true;

// GOOD: Use meaningful conditions
Condition: msg.payload.count < 100;

Unreachable Conditions

Ensure conditions can be met:

// BAD: Condition can never be true
Condition: msg.payload.count > 100 && msg.payload.count < 50;

// GOOD: Use logical conditions
Condition: msg.payload.count >= 0 && msg.payload.count < 100;

Performance Issues

Be careful with expensive condition evaluations:

// BAD: Expensive operation in condition
Condition: processLargeData(msg.payload.data) === "complete";

// GOOD: Use simple property checks
Condition: msg.payload.status === "complete";

Safety Features

Maximum Iteration Limit

Always set a safety limit:

// Configuration
Maximum Iterations: 1000
Condition: msg.payload.count < 10000

// This prevents infinite loops even if the condition is wrong

Timeout Protection

Implement timeout mechanisms:

// Configuration
Maximum Iterations: 100
Timeout: 30000ms // 30 seconds

// Loop will exit after timeout even if condition is still true

Error Handling

Handle errors gracefully:

// Configuration
Condition: msg.payload.count < 100
Error Handling: Continue on error

// Loop continues even if individual iterations fail