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
In JS, it's common in a function that isn't async to chain promises together using .then and .catch
However, it's not clear what the effection equivalent to this is (what happens if you're in a function that isn't a generator? Do you just convert everything to promises using run(myGenerator).then((...) => { ... }) and escape effection temporarily)?
There were two suggestions by Charles on Discord for this:
A simple approach that does not chain together well
Differences with Promise.then: typically you're allowed to chain non-async steps (ex: (async () => 5)().then(a => a+1)). Internally (assuming this is a Promise and not a promise-like), this can be implemented by checking if the return type of fn is instanceof Promise. One can do maybe achieve something similar by checking if fn has a generator symbol, but another approach is to just require using lift instead
Another option is to leverage Task
Note that effection already defines a Task interface that is both a Promise and an Operation. In this sense, we already have a path for implementing this pattern via run() which returns a Task. The problem though is that the implementation of then/catch/finally on the result of run() do NOT return a Task, but rather return just a Promise which means you can't chain it together.
If we instead had an implementation of Task that allowed chaining, this would also solve the problem nicely. Note that effect-ts, for example,
Other projects
Note that the effect-ts library has a similar design decision. They decided that their equivalent to tasks can only be piped, and that once you convert to a promise-like API, you cannot go back
import{Effect}from"effect"consttask1=Effect.succeed(1)// create a task.pipe(Effect.delay("200 millis"));// okay to combine with pipesEffect.runPromise(task1)// convert to promise-like API.then(console.log);
This is different from effection where there is no explicit runPromise, and rather conversion to a promise-like is done lazily if then is ever called
The text was updated successfully, but these errors were encountered:
however, this gets stuck in an infinite loop which I believe is caused by the .enter(), but I'm not fully sure since getting this to work feels like it requires going pretty deep into the inner workings of the continuation library. Maybe the solution is to do something similar to spawn instead?
In JS, it's common in a function that isn't
async
to chain promises together using.then
and.catch
However, it's not clear what the effection equivalent to this is (what happens if you're in a function that isn't a generator? Do you just convert everything to promises using
run(myGenerator).then((...) => { ... })
and escape effection temporarily)?There were two suggestions by Charles on Discord for this:
pipe
function from somewhere (lodash, etc. or write your own)Differences with
Promise.then
: typically you're allowed to chain non-async steps (ex:(async () => 5)().then(a => a+1)
). Internally (assuming this is a Promise and not a promise-like), this can be implemented by checking if the return type offn
isinstanceof Promise
. One can do maybe achieve something similar by checking iffn
has a generator symbol, but another approach is to just require usinglift
insteadTask
Note that effection already defines a
Task
interface that is both a Promise and an Operation. In this sense, we already have a path for implementing this pattern viarun()
which returns a Task. The problem though is that the implementation ofthen
/catch
/finally
on the result ofrun()
do NOT return a Task, but rather return just aPromise
which means you can't chain it together.If we instead had an implementation of
Task
that allowed chaining, this would also solve the problem nicely. Note thateffect-ts
, for example,Other projects
Note that the
effect-ts
library has a similar design decision. They decided that their equivalent to tasks can only be piped, and that once you convert to a promise-like API, you cannot go backThis is different from
effection
where there is no explicitrunPromise
, and rather conversion to a promise-like is done lazily ifthen
is ever calledThe text was updated successfully, but these errors were encountered: