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

Clarify behavior of Promise.reduce when initialValue is undefined. #99

Merged
merged 1 commit into from
Feb 6, 2014

Conversation

cscott
Copy link
Contributor

@cscott cscott commented Feb 6, 2014

If initialValue is a promise that resolves to undefined, it is treated as if initialValue was not supplied at all.

See #97.

@benjamingr
Copy link
Collaborator

👍 you might want to show the analogy of Array::reduce too

@petkaantonov
Copy link
Owner

It is already implied by "if initialValue is not given" which is what undefined means while null means a given empty value. If it's not clear then that should be communicated in general, not under reduce because all methods treat undefined as I just described.

@cscott
Copy link
Contributor Author

cscott commented Feb 6, 2014

@benjamingr it is deliberately different from Array::reduce: [1].reduce(function(){return 2;}, undefined) returns 2, but Promise.reduce([1], function(){return 2}, undefined) returns 1.

@petkaantonov in the above example it is clear to me that "initialValue is given". That is also the language of the es6 specification, https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.reduce, which says, "If an initialValue was provided in the call to reduce". You mean something different, and it is certainly not the case that "all methods treat undefined as I just described". Consider Function.bind(), for example, where f.bind(this) is very different from f.bind(this, undefined).

@petkaantonov
Copy link
Owner

Again, those are ES5 functions not ES6. I meant that the direction taken by ES6 is having these semantics for undefined.

For example when using the default parameter feature of ES6, passing undefined triggers the default parameter. So if function is defined as function a(param = 5) and you do a(undefined) it is same as a(5)

@cscott
Copy link
Contributor Author

cscott commented Feb 6, 2014

If you really wanted to clean up the api, then you should make initialValue a non-optional parameter. That is, reduce should always execute the supplied function exactly as many times as there are elements in the array. That would eliminate a huge potential source of bugs. Then I'd have no problem with the semantics you wish to use for undefined values.

But this particular pull request is just to make the docs match the current behavior. The harmony specs agree with me on this: "providing" or "giving" an argument is different from "argument is undefined". You seem to mean the latter, so the docs should say this clearly.

petkaantonov added a commit that referenced this pull request Feb 6, 2014
Clarify behavior of Promise.reduce when `initialValue` is `undefined`.
@petkaantonov petkaantonov merged commit 4a9a401 into petkaantonov:master Feb 6, 2014
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

Successfully merging this pull request may close these issues.

3 participants