/ examples / js / workflows / conditional-workflow.ts
conditional-workflow.ts
 1  /**
 2   * Conditional Workflow Example
 3   * Demonstrates routing based on conditions
 4   */
 5  
 6  import { route } from 'praisonai';
 7  
 8  async function main() {
 9    const processRequest = async (type: string) => {
10      console.log(`Processing request type: ${type}`);
11  
12      const result = await route([
13        {
14          condition: () => type === 'urgent',
15          execute: async () => {
16            console.log('  -> Urgent handler activated');
17            return 'Processed urgently';
18          }
19        },
20        {
21          condition: () => type === 'normal',
22          execute: async () => {
23            console.log('  -> Normal handler activated');
24            return 'Processed normally';
25          }
26        },
27        {
28          condition: () => true, // Default
29          execute: async () => {
30            console.log('  -> Default handler activated');
31            return 'Processed with default handler';
32          }
33        }
34      ]);
35  
36      return result;
37    };
38  
39    // Test different request types
40    const types = ['urgent', 'normal', 'unknown'];
41    
42    for (const type of types) {
43      const result = await processRequest(type);
44      console.log(`Result: ${result}\n`);
45    }
46  }
47  
48  main().catch(console.error);