Skip to content

Commit

Permalink
add .tap to combining-effects
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed May 14, 2024
1 parent 495b04e commit 4b370e7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
20 changes: 15 additions & 5 deletions content/tutorials/basics/300-combining-effects.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ inside a `pipe` to run the first effect, and then run another effect if the
first one succeeds. Here's an example:

```ts
Effect.succeed(42).pipe(
Effect.andThen((value) => Effect.succeed(value * 2))
)
Effect.succeed(42).pipe(Effect.andThen((value) => Effect.succeed(value * 2)))
```

The result of this effect will be the equivilent of `Effect.succeed(84)`.
The result of this effect will be the equivilent of `Effect.succeed(42 * 2)`.

### Effect.tap

If you want to combine two effects together without altering the return value,
you can use `Effect.tap`. Like `Effect.andThen`, it can be used in a pipeline.
Here's an example:

```ts
Effect.succeed(42).pipe(Effect.tap((value) => Effect.log(value)))
```

The result of this effect will be `42`.

### Exercise

Using `Effect.andThen`, complete the TODO in the editor.
Using `Effect.andThen` & `Effect.tap`, complete the TODOs in the editor.
2 changes: 0 additions & 2 deletions content/tutorials/basics/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
title: Welcome
excerpt: Learn the basics of Effect
section: Learn the basics
workspace: express
---

Welcome to the Effect tutorials!
Expand All @@ -18,7 +17,6 @@ The benefits include:
- Extensive library with a rich ecosystem of packages
- Clustering and Workflows (Alpha)


### How to use this tutorial

To the right is an editor & console window. As you make changes to the code in the editor, the program will re-run and show the output in the console.
Expand Down
12 changes: 10 additions & 2 deletions src/tutorials/basics/300/main.initial.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Effect } from "effect"
import assert from "assert"

Effect.succeed(42).pipe(
// TODO: log the value with `Effect.log`
const multiplyByTwo = (value: number) => Effect.succeed(value * 2)

const promise = Effect.succeed(42).pipe(
// TODO: multiply the value using `multiplyByTwo`
// TODO: log the value with `Effect.log`, without changing the return value
Effect.runPromise
)

promise.then((finalValue) => {
assert.strictEqual(finalValue, 84)
})
12 changes: 10 additions & 2 deletions src/tutorials/basics/300/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Effect } from "effect"
import assert from "assert"

Effect.succeed(42).pipe(
Effect.andThen((value) => Effect.log(value)),
const multiplyByTwo = (value: number) => Effect.succeed(value * 2)

const promise = Effect.succeed(42).pipe(
Effect.andThen(multiplyByTwo),
Effect.tap((value) => Effect.log(value)),
Effect.runPromise
)

promise.then((finalValue) => {
assert.strictEqual(finalValue, 84)
})

0 comments on commit 4b370e7

Please sign in to comment.