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

Change async pipe's default value from null to undefined #39785

Open
wants to merge 1 commit into
base: master
from

Conversation

@josef-kriz
Copy link

@josef-kriz josef-kriz commented Nov 20, 2020

Changes async pipe's default value from null to undefined. It is an incorrect approach to return null when the actual value is unknown. The convention is to use undefined for a variable that has not yet been assigned a value, whereas null is an assignment value and represents no value (more details for example on Stack Overflow).

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.io application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Angular's async pipe subscribes to Observables and Promises and returns the latest value. But when there is no value yet, it returns null. This can lead to some unexpected issues, for example imagine a component with an input like this:

@Input() number?: number

The component can then be used in a template without any problems like this (where myNumber is type of number):

<number-component [number]="myNumber"></number-component>

But when myNumber would be of type Observable<number> and we'd like to use it with the async pipe, it would lead to null being passed to the number component instead of undefined:

<number-component [number]="myNumber | async"></number-component>

A typical use case for this is firing an async HTTP request, displaying some loading element for value of undefined, a value for the actual value, and anything else ("no data", error, ...) for null. The current async pipe breaks this pattern and can cause a headache finding what went wrong as this is not the expected behavior.

Issue Number: #16982

What is the new behavior?

The async pipe now returns undefined by default before an actual value is emitted. It still returns null for inputs of type null and undefined to keep it consistent with other pipes (PR #37447).

This PR follows @mhevery's comment to see how breaking it is. As discussed in the issue #16982, it is not fully clear whether this should be a fix or a feature.

Does this PR introduce a breaking change?

  • Yes
  • No

The async pipe now returns undefined as the default value. Requires a fix if you performed a check for the null value before, but this check was likely needed solely because of this unconventional behavior.

Other information

I've also updated the doc comment that this pipe returns undefined before the first emit, as I was lacking this information about null there before.

@google-cla
Copy link

@google-cla google-cla bot commented Nov 20, 2020

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added the cla: no label Nov 20, 2020
@pullapprove pullapprove bot requested a review from jessicajaniuk Nov 20, 2020
@josef-kriz josef-kriz force-pushed the josef-kriz:fix/async-pipe-default-undefined branch from 2ecf20d to e3b0aa5 Nov 20, 2020
@google-cla google-cla bot added cla: yes and removed cla: no labels Nov 20, 2020
@AndrewKushnir AndrewKushnir requested a review from mhevery Nov 20, 2020
@ngbot ngbot bot added this to the needsTriage milestone Nov 20, 2020
@mhevery
Copy link
Member

@mhevery mhevery commented Nov 20, 2020

You are correct that undefined would be preferred. Due to complex set of historical reasons, which are no longer true we had to chose null. Those historical reasons no longer apply and changing it to undefined would be nice, however this is a pretty big undertaking.

Could you get the tests green? Once the tests are green we can try to see what it would take to land it in Google codebase, but I suspect it will not be trivial.

@mhevery mhevery self-assigned this Nov 20, 2020
@google-cla
Copy link

@google-cla google-cla bot commented Nov 20, 2020

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@google-cla google-cla bot added cla: no and removed cla: yes labels Nov 20, 2020
It is an incorrect approach to return `null` when the actual value is unknown. The convention is to use `undefined`
 for a variable that has not yet been assigned a value, whereas `null` is an assignment value and represents no value.

BREAKING CHANGE:

The async pipe now returns `undefined` as the default value. Requires a fix when relying on the async pipe
 returning `null` as the default value.

Fixes #16982
@mhevery mhevery force-pushed the josef-kriz:fix/async-pipe-default-undefined branch from 42a8dd7 to 5397cc9 Nov 20, 2020
@google-cla google-cla bot added cla: yes and removed cla: no labels Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants
You can’t perform that action at this time.