You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A solution to this is to use the same run (aka the same Task) multiple times instead of creating a new task every time
constfunc=function*(){console.log("compute");return5;}conststarted=func();conststarted2=run(()=>started)// save the `Task` to reuse itconsole.log(awaitstarted2);// returns 5console.log(awaitstarted2);// returns 5 again!main(function*(){// and this can even be used as a generator!console.log(yield*started2);// still 5console.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
In typescript, a very common pattern is to share a promise in multiple places. For example:
However, this can't be done directly with Operation as they can only be computed once:
Solution
A solution to this is to use the same
run
(aka the sameTask
) multiple times instead of creating a new task every timeThat 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 #944Other options
I noticed that
effect-ts
handles this differently by having afork
andjoin
command: https://effect.website/docs/additional-resources/effect-vs-promise/#faqThe text was updated successfully, but these errors were encountered: