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

Promise.then() is difficult to use with lambdas #165

Open
aalmiray opened this issue Mar 19, 2018 · 5 comments
Open

Promise.then() is difficult to use with lambdas #165

aalmiray opened this issue Mar 19, 2018 · 5 comments

Comments

@aalmiray
Copy link
Contributor

aalmiray commented Mar 19, 2018

Currently Promise.then takes 3 different types of inputs: DoneCallback, DoneFilter, and DonePipe. This forces explicit casts when using lambda expressions. It would be better if this method were to be split in 3 variants, such as

then(DoneCallback cbk)
thenFilter(DoneFilter fltr)
thenPipe(DonePipe pipe)
@sclassen
Copy link
Contributor

filter() is actually misleading term here as the operation executed here is mostly referred to as map().
So maybe all the filter need to be renamed to follow common naming and help to reduce the learning curve.

@aalmiray
Copy link
Contributor Author

That's a good point @sclassen. This is the right time to provide an alias and deprecate filter or do a full rename given that we're braking binary compatibility.

@sclassen
Copy link
Contributor

sclassen commented Oct 1, 2018

My proposal for renaming (based on the naming in java streams and vavr.io):
filer -> map
pipe -> flatMap

The code would then read as follows:

someCallToDbReturningAPromise(id)
    .map(t -> t.name())
    .flatMap(name -> callSomeBackgroundTaskReturningAPromise(name))
    .then(result -> saveToMap(result));

@aalmiray
Copy link
Contributor Author

aalmiray commented Oct 1, 2018

I get s/filter/map/ but isn't pipe a variation of mapas well?

@sclassen
Copy link
Contributor

sclassen commented Oct 1, 2018

yes.
DoneFilter returns D_OUT which is then wrapped in a new Promise<D_OUT, F, P>, where as DonePipe returns Promise<D_OUT, F_OUT, P_OUT>
So if we would apply the DonePipe as a map() we would get Promise<Promise<D_OUT, F_OUT, P_OUT>, F, P>
Because this is just complicated and does not add any value the pipe operation unwraps the inner promise.
The same does the flatMap() on streams. it unwraps a stream in a stream -> it flattens the generic...

Does this explanation help?

saturnism pushed a commit that referenced this issue Nov 14, 2018
* Provide aliases for filter/pipe. Fix for #165
* Fully rename pipe/filter methods instead of providing aliases
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

2 participants