Tests can be any value. If the test is a function, it is executed and the return value is used (so it must be synchronous). Otherwise, the test is used directly. If the value is truethy then the test passes.
aif(true,cb=>cb('A'))
.else(cb=>cb('B'))
.finally(res=>console.log(res));
// prints A
aif('zz',cb=>cb('A'))
.else(cb=>cb('B'))
.finally(res=>console.log(res));
// prints A
aif(null,cb=>cb('A'))
.else(cb=>cb('B'))
.finally(res=>console.log(res));
// prints B
functionmyFunc(){returntrue;}
aif(myFunc,cb=>cb('A'))
.else(cb=>cb('B'))
.finally(res=>console.log(res));
// prints A
constv=2;
aif(()=> v <0,cb=>cb('A'))
.else(cb=>cb('B'))
.finally(res=>console.log(res));
// prints B
Callbacks
if, elseIf, and else statement execution functions are passed a callback. This callback should be called exactly once. Once the callback is called, the finally execution function is called with the same arguments passed to the callback.
aif(true,cb=>cb('A',1))
.elseIf(true,cb=>cb('B',null,2))
.else(cb=>cb('C',[]))
.finally((...args)=>console.log(args));
// prints ['A', 1]
aif(false,cb=>cb('A',1))
.elseIf(true,cb=>cb('B',null,2))
.else(cb=>cb('C',[]))
.finally((...args)=>console.log(args));
// prints ['B', null, 2]
aif(false,cb=>cb('A',1))
.elseIf(false,cb=>cb('B',null,2))
.else(cb=>cb('C',[]))
.finally((...args)=>console.log(args));
// prints ['C', []]
Errors
If you pass something that is not a function as the statement execution function, an error will be thrown.
If you call a callback more than once, an error will be thrown.