Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section for flow on "Building Pipelines" #821

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions content/docs/400-guides/100-essentials/600-pipeline.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,34 @@ const program = Effect.all([fetchTransactionAmount, fetchDiscountRate]).pipe(
)
```

## The flow method

Sometimes, you may need to create a reusable pipeline of operations. In such
cases, you can use the `flow` method.

```ts twoslash
import { Effect, Duration, flow } from "effect"

const myRetryLogic = flow(
Effect.timeout(Duration.seconds(3)),
Effect.tapError((error) => Effect.log(`Failed: ${error}`)),
Effect.retry({ times: 2 }),
Effect.tapError((error) => Effect.log(`Abandon as retry failed: ${error}`)),
)

const fetchTransactionAmount = Effect.promise(() => Promise.resolve(100))
const fetchDiscountRate = Effect.promise(() => Promise.resolve(5))

const fetchTransactionAmountWithRetry = fetchTransactionAmount.pipe(myRetryLogic)
const fetchDiscountRateWithRetry = fetchDiscountRate.pipe(myRetryLogic)
```

<Info>
`flow` and `pipe` are very similar. The main difference is that `flow` returns *a function that returns an Effect*
thanhnguyen2187 marked this conversation as resolved.
Show resolved Hide resolved
(`() => Effect`), while `pipe` returns an `Effect`. It suggests that `pipe` should be used when you want a result,
thanhnguyen2187 marked this conversation as resolved.
Show resolved Hide resolved
while `flow` should be used for further composition.
</Info>

## Cheatsheet

Let's summarize the transformation functions we have seen so far:
Expand Down