0.0.9 • Published 10 months ago

@trytrench/flow v0.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

Flow: TypeScript Promise DAG Execution

Flow enables you to define type-safe Promise Directed Acyclic Graphs (DAGs) and execute them in the most efficient order.

Installation

npm i @trytrench/flow

Using Flow

Here is how you can utilize Flow in your code:

Step 1: Initialize Node Builder

First, initialize a Node Builder with a specific NodeInput type:

import { initNodeBuilder } from "@trytrench/flow";

type NodeInput = {
  timestamp: Date;
};

const nodeBuilder = initNodeBuilder.input<NodeInput>().create();

Step 2: Create Nodes

Next, create the nodes. Here we create temperatureNode and windSpeedNode that fetch data asynchronously from APIs. We then create windChillNode that depends on temperatureNode and windSpeedNode, and calculates the wind chill factor:

const temperatureNode = nodeBuilder
  .resolver(async ({ input }) => {
    const temperature = await fetchTemperatureFromApi(input.timestamp);
    return temperature;
  });

const windSpeedNode = nodeBuilder
  .resolver(async ({ input }) => {
    const windSpeed = await fetchWindSpeedFromApi(input.timestamp);
    return windSpeed;
  });

const windChillNode = nodeBuilder
  .depend({
    temperature: temperatureNode,
    windSpeed: windSpeedNode,
  })
  .resolver(({ deps }) => {
    const { temperature, windSpeed } = deps;
    const windChillFactor = calculateWindChill(temperature, windSpeed);
    return windChillFactor;
  });

Step 3: Run Node

Finally, run the nodes. In this case, we're executing windChillNode and logging the returned wind chill factor:

windChillNode
  .run({
    timestamp: new Date(),
  })
  .then((windChillFactor) => {
    console.log(`The wind chill factor is: ${windChillFactor}`);
  })
  .catch((err) => {
    console.error(err);
  });
0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago

1.0.0

11 months ago