-
Notifications
You must be signed in to change notification settings - Fork 110
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
pipeline-equals assignment #125
Comments
I think that this could be useful for cases where you want to pass properties through the pipeline. This would solve a similar problem to A Promises example:I think it's easier to exemplify with a working Promises example. This is a real world scenario: You need to read some items from storage, set some configuration, and make a network request -- all async operations. getItemsAsync().then(items =>
setConfigAsync(items).then(
() => httpGet(items),
),
); This creates more rightward drift in the code. Alternatively you could use let oitems;
getItemsAsync().then(items => {
oitems = items;
return setConfigAsync(items);
}).then(() => httpGet(oitems));
const items = await getItemsAsync();
await setConfigAsync(items);
return httpGet(items); I think this is much easier to reason about, and it doesn't require "carrying values around," if that makes sense. Applying to the pipelineImagine an Observable chain that does the same operations: getItems$().pipe(
mergeMap(items => setConfig$(items).pipe(
mergeMap(() => httpGet$(items)),
),
); ... alternatively ... getItems$().pipe(
mergeMap(items => setConfig$(items), (_, items) => items),
mergeMap(httpGet$),
); If the chain gets longer, the carrying around of items becomes more cumbersome. You can see complaints about this here: ReactiveX/RxJava#2931 and quite a few other places. Solution using the pipeline and assignmentIf you can do assignment in the pipeline this problem is solved by keeping the values in the same scope as the other operators: // `of` is used here to kick off the Observable chain
of(1)
const items |>= mergeMap(() => getItems$()),
|> mergeMap(() => setConfig$(items)),
|> mergeMap(() => httpGet$(items)), This syntax isn't perfect, but I think the general idea is good. |
I'd like to leave this idea for a follow-on proposal, and leave the initial proposal simpler and minimal. What do you think? |
@ajcrites Hey, I see your reasoning for wanting this though for your scenario
which is the same as:
The composition, in my opinion, would be better composed as 'something that happens outside of an observable' which can then be called from an 'observable':
no (in other words, you wouldn't even need observable chains involved, for process, in javascript)? // Updated example above Same as example above but without sequences:
|
In the proposal I saw no mention of something like
variable |>= fn
as a clean shorthand forvariable = fn(variable)
, so I thought I'd bring it up so it can be added to the spec with this proposal, and doesn't bite us later.You might want to discourage it as JS is going more and more functional, but if so, it would be nice to have that written in the proposal as well
The text was updated successfully, but these errors were encountered: