* Conditional execution based on a condition * @input any * @param condition - Boolean condition to evaluate * @param thenAction - Action to execute if condition is true * @param elseAction - Action to execute if condition is false * @returns Result of either thenAction or elseAction
(
ctx: ChibiCtx,
input: void,
condition: ChibiJson<boolean>,
thenAction: Then,
elseAction: Else,
)
| 18 | * ).concat('world').run(); // returns 'helloworld' |
| 19 | */ |
| 20 | if<Input, Then extends ChibiJson<any>, Else extends ChibiJson<any>>( |
| 21 | ctx: ChibiCtx, |
| 22 | input: void, |
| 23 | condition: ChibiJson<boolean>, |
| 24 | thenAction: Then, |
| 25 | elseAction: Else, |
| 26 | ): CT.UnwrapJson<Then> | CT.UnwrapJson<Else> { |
| 27 | if (ctx.isAsync()) { |
| 28 | return (async () => { |
| 29 | const conditionState = await ctx.runAsync(condition); |
| 30 | if (conditionState) { |
| 31 | return ctx.runAsync(thenAction); |
| 32 | } |
| 33 | return ctx.runAsync(elseAction); |
| 34 | })() as any; |
| 35 | } |
| 36 | |
| 37 | const conditionState = ctx.run(condition); |
| 38 | if (conditionState) { |
| 39 | return ctx.run(thenAction); |
| 40 | } |
| 41 | return ctx.run(elseAction); |
| 42 | }, |
| 43 | |
| 44 | /** |
| 45 | * Conditional execution based on a condition |