diff --git a/content/tutorials/basics/300-combining-effects.mdx b/content/tutorials/basics/300-combining-effects.mdx index c09a49464..85f292ce7 100644 --- a/content/tutorials/basics/300-combining-effects.mdx +++ b/content/tutorials/basics/300-combining-effects.mdx @@ -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. \ No newline at end of file +Using `Effect.andThen` & `Effect.tap`, complete the TODOs in the editor. diff --git a/content/tutorials/basics/index.mdx b/content/tutorials/basics/index.mdx index b50927672..ff83b1e1a 100644 --- a/content/tutorials/basics/index.mdx +++ b/content/tutorials/basics/index.mdx @@ -2,7 +2,6 @@ title: Welcome excerpt: Learn the basics of Effect section: Learn the basics -workspace: express --- Welcome to the Effect tutorials! @@ -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. diff --git a/src/tutorials/basics/300/main.initial.txt b/src/tutorials/basics/300/main.initial.txt index 160e800e0..5be1efe0c 100644 --- a/src/tutorials/basics/300/main.initial.txt +++ b/src/tutorials/basics/300/main.initial.txt @@ -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) +}) diff --git a/src/tutorials/basics/300/main.ts b/src/tutorials/basics/300/main.ts index 790d27844..330549697 100644 --- a/src/tutorials/basics/300/main.ts +++ b/src/tutorials/basics/300/main.ts @@ -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) +})