import { Callout } from "nextra/components"; import REPL from "../../components/REPL";
The `compose` function is equivalent to `a(b(c(val)))`.Performs functions composition from right to left asynchronously.
Related
import { aCompose } from '@opentf/std';
aCompose(
val: unknown,
...fns: Function[]
): Promise<unknown>
await aCompose(
1,
(x) => Promise.resolve(x + 1),
(x) => Promise.resolve(x * 5)
); //=> 6
<REPL code={`const { aCompose } = require('@opentf/std');
async function main() { const out = await aCompose(1, (x) => Promise.resolve(x + 1), (x) => Promise.resolve(x * 5) ); log(out); }
main(); `} />
Why we need function composition?
- The deep nesting of functions is hard to read.
- It eliminates temporary variables.
- Method chaining is limited, for Eg: await, yeild, etc.