> ## Documentation Index
> Fetch the complete documentation index at: https://docs.metz.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Flows-102

> Anatomy of a Flow

We haven't covered the [standard library](https://localhost:3002/standard-library#flow) yet, but for now, here are the basics: `std` is a globally available
module, there to help you interact with the runtime.

You create flows using `std.flow`. It takes two parameters:

1. Name of the flow
2. An instance of a class

`std.flow` lets you go through the looking glass. It lets you provide a new [state machine](/runtime#state-machines) to the runtime, such that
when it [ticks](/runtime#tick), it manages this newly given state machine as well.

## FlowExecutor

<Frame caption="A standard flow, lazily evaluated.">
  <img src="https://mintcdn.com/metzsh/fELY9edQkrehV0tS/images/std-flow.png?fit=max&auto=format&n=fELY9edQkrehV0tS&q=85&s=dcb7385c6def8d191fd65381e0cdf2fa" width="2245" height="1587" data-path="images/std-flow.png" />
</Frame>

<br />

<AccordionGroup>
  <Accordion title="What's a FlowGenerator?">
    `std.flow` returns a `FlowGenerator`, which is an object that closely resembles the class instance that you provided.
    With one key difference, it still has methods, but all of them are `FlowFunction`s.
  </Accordion>

  <Accordion title="What's a FlowFunction?">
    This is as close as we can get to referring the underlying state-machine. A `FlowFunction` represents a factory that
    can produce one.

    It returns a `ScheduledFlow` if a schedule is provided to `std.flow`, or a `FlowExecutor` if not.
  </Accordion>
</AccordionGroup>

When you don't provide a schedule to `std.flow` and call a `FlowFunction`, you get a `FlowExecutor`. This is a handle that encapsulates work.

A `FlowExecutor` doesn't start its work unless explcitly told to do so. It has two function through which
you can start execution:

```typescript theme={null}
interface FlowExecutor<RT, M> {
	run(): void;
	await(): RT;
}
```

<Info>We will look at `await` in next section.</Info>
The type argument `RT` captures the return type of the [FlowFunction](/flows-102#flowfunction) that spawned it, `M` we can ignore for now.

Given a `FlowExecutor` is a handle, we can do this:

```typescript theme={null}
class Main {
	hello() {
		const flow = std.flow<Main>("Print!", this).world("Hello");
		this.print(flow); //We didn't call 'run' on flow, but gave it to 'print'.
	}

	world(arg: string) {
		std.log(`${arg} World!`);
	}

	// This takes a flow, and simply runs it.
	print(flow: std.FlowExecutor<void, Main['world']>) {
		flow.run();
	}
}
```

Try this code in the [playground](https://app.metz.sh/try) to see what happens!

<Warning>You can pass around flows like values, but be mindful of type arguments. They are essential to creating
the call graph.</Warning>

## Scheduled Flows

<Frame caption="A scheduled flow, eagerly evaluated, run immediately.">
  <img src="https://mintcdn.com/metzsh/fELY9edQkrehV0tS/images/std-scheduled-flow.png?fit=max&auto=format&n=fELY9edQkrehV0tS&q=85&s=71f10edcd1c334812cfc0d13c1d252e1" width="2245" height="1587" data-path="images/std-scheduled-flow.png" />
</Frame>

You can provide a schedule to `std.flow` to get a `ScheduledFlow`. A `schedule` is an object and it can
have <Tooltip tip="But not both!">either</Tooltip> of the following keys:

<ParamField path="after" type="number">
  Start the flow after given number of `ticks` have passed.
</ParamField>

<ParamField path="every" type="number">
  Start a flow every time given number of `ticks` pass.
</ParamField>

A `ScheduledFlow` is evaluated immediately, and it begins its work in the same tick it's created.
<br />Check out the [playground](https://app.metz.sh/try?templateId=4) to see how we can emulate a polling job using it.
