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

Added the FAQ section #968

Merged
merged 9 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
47 changes: 47 additions & 0 deletions www/docs/faq.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
This page covers questions users of Effection ask as they go through the journey
of learning Effection.

## Why can't I `yield*` to a promise?

In TypeScript, if you tried to `yield* Promise.resolve(42)`, TypeScript compiler will
throw an error `Type 'Promise<number>' must have a '[Symbol.iterator]()' method that returns an iterator.`.
In JavaScript, it will just not work. This happens because neither the TypeScript nor the JavaScript runtimes know how to
convert a Promise into an iterator, which is required for yield* to work.

While adding the integration to make `yield*` work with a promise is possible by monkey patching the `Promise` prototype, we opted not to do so. Not only is monkey patching a global object from a library considered bad form, but it would also compromise the guarantees that are described in [Thinking in Effection](./thinking-in-effection)
by blurring the line about where those guarantees apply. Right now, the line is very clear: anything on the
right side of `yield*` is guaranteed to exit fully and never outlive the operation where the `yield*` was
called. We can't make these guarantees with Promises for the reasons described in [The Await Event Horizon](https://frontside.com/blog/2023-12-11-await-event-horizon/)
blog post.

## Can I make Promise compatible with `yield*`?

Yes. You can `yield*` to a `Promise` by monkey patching the `Promise` prototype. "Monkey patching" in JavaScript refers to the practice
of modifying or extending existing objects or functions at runtime, typically without changing their
original source code. It's considered risky because it breaks the expectations that developers maintaining
and running the code have of the JavaScript runtime. There are circumstances where the convenience of `yield*`
to promise outweighs the risks. We'll let you make the decision where those circumstances are for you.

You can use the following code to monkeypatch Promise if you choose to do so. You can put this code into a module
and import it when you want this effect.

```typescript
import { call, type Effect } from "effection";

declare global {
interface Promise<T> extends Operation<T> {}
}

Object.defineProperty(Promise.prototype, Symbol.iterator, {
value(): {
return call(() => this)[Symbol.iterator]();
}
});
```

## Why can't I use `call(Promise.resolve(42))` anymore?

In Effection v3, it was possible to `yield* call(Promise.resolve(42))` but we removed this in Effection v4
because we wanted to align call closer to `Function.prototype.call`. You can still use
`yield* call(() => Promise.resolve(42))` and `yield* call(async function() {})`. In v4, we plan to ship
a new function called `when` which will take a promise and wrap it in a constructor for convenience.
1 change: 1 addition & 0 deletions www/docs/structure.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
["context.mdx", "Context"]
],
"Advanced": [
["faq.mdx", "FAQ"],
["scope.mdx", "Scope"],
["processes.mdx", "Processes"]
]
Expand Down