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

Using Generators: add Passing this #330

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Changes from all commits
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
21 changes: 21 additions & 0 deletions pages/docs/essentials/using-generators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,24 @@ const program = Effect.gen(function* (_) {
// ...
})
```

## 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 /this/
import { Effect } from "effect"

class MyService {
readonly local = 1
compute() {
return Effect.gen(this, function* (_) {
return yield* _(Effect.succeed(this.local + 1))
})
}
}

console.log(Effect.runSync(new MyService().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.
Loading