Optional
args: ArgsTypeconst result = run([
() => 10,
() => 2,
() => 3
]);
// Yields: 10
Options can be passed for evaluation:
const result = run([
(args) => {
if (args === 'apple') return 100;
},
() => {
return 10;
}
])
const expr = [
(opts) => 10,
(opts) => 2,
(opts) => 3
];
const opts = {
rank: (a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
}
const result = await run(expr, opts);
// Returns: 2
In terms of typing, it takes an generic arguments ArgsType
and ResultType
:
ArgsType
: type of expression arguments. This might be void
if no arguments are used.ResultType
: return type of expression functionsThus the expressions
parameter is an array of functions:
(args:ArgsType|undefined) => ResultType|undefined
// or
(args:ArgsType|undefined) => Promise<ResultType|undefined>
Example:
const expressions = [
// Function takes a string arg
(args:string) => return true; // boolean is the necessary return type
];
const run<string,boolean>(expressions, opts, 'hello');
Runs a series of async expressions, returning the results. Use runSingle if it's only a single result you care about.