Skip to content

Latest commit

 

History

History
68 lines (49 loc) · 1.28 KB

aCompose.mdx

File metadata and controls

68 lines (49 loc) · 1.28 KB

import { Callout } from "nextra/components"; import REPL from "../../components/REPL";

Performs functions composition from right to left asynchronously.

The `compose` function is equivalent to `a(b(c(val)))`.

Related

Syntax

import { aCompose } from '@opentf/std';

aCompose(
  val: unknown,
  ...fns: Function[]
): Promise<unknown>

Examples

await aCompose(
  1,
  (x) => Promise.resolve(x + 1),
  (x) => Promise.resolve(x * 5)
); //=> 6

Try

<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(); `} />

Learn

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.

Resources