Using Generators in Effect
On this page
In the previous sections, we learned how to create effects and execute them. Now, it's time to write our first simple program.
Effect offers a convenient syntax, similar to async
/await
, to write effectful code using generators.
The use of generators is an optional feature in Effect. If you find generators unfamiliar or prefer a different coding style, you can explore the documentation about Building Pipelines in Effect.
Understanding Effect.gen
Let's start with a basic program that performs a series of transformations:
ts
import {Effect } from "effect"constincrement = (x : number) =>x + 1constdivide = (a : number,b : number):Effect .Effect <number,Error > =>b === 0?Effect .fail (newError ("Cannot divide by zero")):Effect .succeed (a /b )consttask1 =Effect .promise (() =>Promise .resolve (10))consttask2 =Effect .promise (() =>Promise .resolve (2))export constprogram =Effect .gen (function* () {consta = yield*task1 constb = yield*task2 constn1 = yield*divide (a ,b )constn2 =increment (n1 )return `Result is: ${n2 }`})Effect .runPromise (program ).then (console .log ) // Output: "Result is: 6"
ts
import {Effect } from "effect"constincrement = (x : number) =>x + 1constdivide = (a : number,b : number):Effect .Effect <number,Error > =>b === 0?Effect .fail (newError ("Cannot divide by zero")):Effect .succeed (a /b )consttask1 =Effect .promise (() =>Promise .resolve (10))consttask2 =Effect .promise (() =>Promise .resolve (2))export constprogram =Effect .gen (function* () {consta = yield*task1 constb = yield*task2 constn1 = yield*divide (a ,b )constn2 =increment (n1 )return `Result is: ${n2 }`})Effect .runPromise (program ).then (console .log ) // Output: "Result is: 6"
The generator API is only available when using the downlevelIteration
flag
or with a target
of "es2015"
or higher in your tsconfig.json
file
Comparing Effect.gen with async/await
If you are familiar with async
/await
, you may notice that the flow of writing code is similar.
Let's compare the two approaches:
ts
constincrement = (x : number) =>x + 1constdivide = (a : number,b : number):Effect .Effect <number,Error > =>b === 0?Effect .fail (newError ("Cannot divide by zero")):Effect .succeed (a /b )consttask1 =Effect .promise (() =>Promise .resolve (10))consttask2 =Effect .promise (() =>Promise .resolve (2))export constprogram =Effect .gen (function* () {consta = yield*task1 constb = yield*task2 constn1 = yield*divide (a ,b )constn2 =increment (n1 )return `Result is: ${n2 }`})Effect .runPromise (program ).then (console .log ) // Output: "Result is: 6"
ts
constincrement = (x : number) =>x + 1constdivide = (a : number,b : number):Effect .Effect <number,Error > =>b === 0?Effect .fail (newError ("Cannot divide by zero")):Effect .succeed (a /b )consttask1 =Effect .promise (() =>Promise .resolve (10))consttask2 =Effect .promise (() =>Promise .resolve (2))export constprogram =Effect .gen (function* () {consta = yield*task1 constb = yield*task2 constn1 = yield*divide (a ,b )constn2 =increment (n1 )return `Result is: ${n2 }`})Effect .runPromise (program ).then (console .log ) // Output: "Result is: 6"
It's important to note that although the code appears similar, the two programs are not identical. The purpose of comparing them side by side is just to highlight the resemblance in how they are written.
Embracing Control Flow
One significant advantage of using Effect.gen
in conjunction with generators is its capability to employ standard control flow constructs within the generator function. These constructs encompass if
/else
, for
, while
, and other branching and looping mechanisms, enhancing your ability to express complex control flow logic in your code.
To illustrate this, let's delve into a practical example:
ts
import {Effect } from "effect"constdivide = (a : number,b : number):Effect .Effect <number,Error > =>b === 0?Effect .fail (newError ("Cannot divide by zero")):Effect .succeed (a /b )constprogram =Effect .gen (function* () {leti = 1while (true) {if (i === 10) {break} else {if (i % 2 === 0) {console .log (yield*divide (12,i ))}i ++continue}}})Effect .runPromise (program )/*Output:6321.5*/
ts
import {Effect } from "effect"constdivide = (a : number,b : number):Effect .Effect <number,Error > =>b === 0?Effect .fail (newError ("Cannot divide by zero")):Effect .succeed (a /b )constprogram =Effect .gen (function* () {leti = 1while (true) {if (i === 10) {break} else {if (i % 2 === 0) {console .log (yield*divide (12,i ))}i ++continue}}})Effect .runPromise (program )/*Output:6321.5*/
Raising Errors
Within the realm of the Effect.gen
API, you have the capability to introduce errors into your program by yielding a failed effect. This can be effectively achieved, for instance, by using Effect.fail
. Let's take a closer look at an example:
ts
import {Effect } from "effect"constprogram =Effect .gen (function* () {console .log ("Task1...")console .log ("Task2...")yield*Effect .fail ("Something went wrong!")})Effect .runPromiseExit (program ).then (console .log )/*Output:Task1...Task2...{_id: 'Exit',_tag: 'Failure',cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong!' }}*/
ts
import {Effect } from "effect"constprogram =Effect .gen (function* () {console .log ("Task1...")console .log ("Task2...")yield*Effect .fail ("Something went wrong!")})Effect .runPromiseExit (program ).then (console .log )/*Output:Task1...Task2...{_id: 'Exit',_tag: 'Failure',cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong!' }}*/
In this example, we intentionally introduce an error using Effect.fail
to illustrate how errors can be managed within your program.
The Role of Short-Circuiting
When working with the Effect.gen
API, it's important to understand how it manages errors.
This API is designed to short-circuit the execution upon encountering the first error.
What does this mean for you as a developer? Well, let's say you have a chain of operations or a collection of effects to be executed in sequence. If any error occurs during the execution of one of these effects, the remaining computations will be skipped, and the error will be propagated to the final result.
In simpler terms, the short-circuiting behavior ensures that if something goes wrong at any step of your program it will immediately stop and return the error to let you know that something went wrong.
ts
import {Effect } from "effect"constprogram =Effect .gen (function* () {console .log ("Task1...")console .log ("Task2...")yield*Effect .fail ("Something went wrong!")console .log ("This won't be executed")})Effect .runPromise (program ).then (console .log ,console .error )/*Output:Task1...Task2...{_id: 'Exit',_tag: 'Failure',cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong!' }}*/
ts
import {Effect } from "effect"constprogram =Effect .gen (function* () {console .log ("Task1...")console .log ("Task2...")yield*Effect .fail ("Something went wrong!")console .log ("This won't be executed")})Effect .runPromise (program ).then (console .log ,console .error )/*Output:Task1...Task2...{_id: 'Exit',_tag: 'Failure',cause: { _id: 'Cause', _tag: 'Fail', failure: 'Something went wrong!' }}*/
If you want to dive deeper into effective error handling with Effect, you can explore the "Error Management" section.
Passing this
In some cases, you might need to pass a reference to the current object (this
) into the body of your generator function. You can achieve this by utilizing an overload that accepts the reference as the first argument:
ts
import {Effect } from "effect"classMyService {readonlylocal = 1compute =Effect .gen (this, function* () {return yield*Effect .succeed (this.local + 1)})}console .log (Effect .runSync (newMyService ().compute )) // Output: 2
ts
import {Effect } from "effect"classMyService {readonlylocal = 1compute =Effect .gen (this, function* () {return yield*Effect .succeed (this.local + 1)})}console .log (Effect .runSync (newMyService ().compute )) // Output: 2
In this example, we have a MyService
class with a property called local
. By passing this
as the first argument to Effect.gen
, we make the local
property available within the generator.