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 Operation } 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. 

## How can I chain operations together like I do with promises?

In JS, it's common in a function that isn't async to chain promises together using `.then()` and `.catch()`

The simplest way is to use the [Chain](https://frontside.com/effection/x/chain/) helper from [effectionx](https://frontside.com/effection/x)

```typescript
import { Chain } from "@effectionx/chain";

Chain.from(operation)
  .then(lift((val) => val * 2)),
  .then(lift((val) => String(val)),
  .then(lift((val) => val.toUpperCase())),
```

Note the usage of [`lift()`](../../api/v4/lift) to convert normal synchronous functions into operations.

But composition is so simple, that rolling your own is almost trivial.
For example, you can make make a "pipeable" `then()` operation like
so:

```typescript
function then<A,B>(fn: (value: A) => Operation<B>): (operation: Operation<A>) => Operation<B> {
  return function*(operation) {
    return yield* fn(yield* operation);
  }
}
```

You can then take an off the shelf pipe function (they're a dime a
dozen from places like lodash, fp-ts, or even the code you have up your
own sleeve) and compose several `then()` statements together.

```ts
pipe(
  operation,
  then(lift((val) => val * 2)),
  then(lift((val) => String(val)),
  then(lift((val) => val.toUpperCase())),
);
```

## How do I share the outcome of an operation in several places?

A very common pattern is to share a promise in multiple places. For example:

```ts
class Foo {
  data: Promise<number>;

  constructor() {
    this.data = (async () => 5)();
  }

  async getData() {
    return await this.data;
  }
}

const foo = new Foo();
console.log(await foo.getData());
```

However, this can't be done directly with Operation as they can only be computed once:

```ts
const func = function* () {
  console.log("compute");
  return 5;
}

const started = func();
```

If this is something you need, you can use the [`Chain.from`]() method from the [@effectionx/chain]() package which only evaluate its operation once and share it between all chains that are derived from it:


```ts
class Foo {
  data: Promise<number>;

  constructor() {
    let operation = (function* () {
      return 5;
    })();
    this.data = Chain.from(operation);
  }

  *getData() {
    return yield* this.data;
  }
}

const foo = new Foo();
console.log(yield * foo.getData());
```
