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

Document equivalent to shared Promise #946

Open
SebastienGllmt opened this issue Dec 21, 2024 · 0 comments
Open

Document equivalent to shared Promise #946

SebastienGllmt opened this issue Dec 21, 2024 · 0 comments

Comments

@SebastienGllmt
Copy link

SebastienGllmt commented Dec 21, 2024

In typescript, a very common pattern is to share a promise in multiple places. For example:

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:

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

const started = func();

console.log(await run(() => started)); // returns 5
console.log(await run(() => started)); // returns undefined

Solution

A solution to this is to use the same run (aka the same Task) multiple times instead of creating a new task every time

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

const started = func();
const started2 = run(() => started) // save the `Task` to reuse it

console.log(await started2); // returns 5
console.log(await started2); // returns 5 again!

main(function* () {
  // and this can even be used as a generator!
  console.log(yield* started2); // still 5
  console.log(yield* started2); // still 5
});

That is to say, I think Task is the go-to when you want this kind of Promise-like behavior. The only caveat to this approach is that it doesn't work when you try to chain the result of the task as described in solution (3) in #944

Other options

I noticed that effect-ts handles this differently by having a fork and join command: https://effect.website/docs/additional-resources/effect-vs-promise/#faq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant