Skip to content

Warn on negated async pipes #44867

Closed as not planned
Closed as not planned

Description

@dgp1130

Which @angular/* package(s) are relevant/releated to the feature request?

@angular/compiler-cli

Description

Negating an async pipe in an *ngIf thrashes the layout because the pipe emits null immediately before checking the condition. This can lead cause unnecessary rendering work and trigger component events which cause XHR's or other work to be done that shouldn't be needed.

<div *ngIf="!(myConditional | async)">
  <!-- ... -->
</div>

http://codelyzer.com/rules/template-no-negated-async/

Proposed solution

We should consider an extended diagnostic to detect and warn the user of this anti-pattern to head-off their confusing and suggest a better alternative.

I'm not entirely sure what that alternative should be, but I imagine there's an easily transformable way of doing this?

Alternatives considered

N/A

Activity

  1. added
    feature
    area: compiler
    on Jan 28, 2022
  2. added this to the Backlog milestone on Jan 28, 2022
  3. angular-robot commented on Jan 28, 2022

    @angular-robot
    Contributor

    This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.

    You can find more details about the feature request process in our documentation.

  4. amakhrov commented on Jan 31, 2022

    @amakhrov

    This sounds very valuable for "truely async" observables.
    However, it's likely to be less helpful on projects using observable state management (eg NGRX), where the state always has some current, synchronous value.

  5. kirillgroshkov commented on Feb 10, 2022

    @kirillgroshkov

    I don't understand how "negated async" vs "non-negated async" causes more "thrashing"?

    In case of "negated async" for smth that is not-null it'll change from "shown" to "hidden".

    For "non-negated async" it'll change from "hidden" to "shown".

    Both ways is a change in DOM.

    What makes the first case worse than the second?

    E.g I'm thinking in terms of CLS (cumulative layout shift) - it's the same (just different "direction" of the shift).

  6. dgp1130 commented on Feb 10, 2022

    @dgp1130
    ContributorAuthor

    @kirillgroshkov, my understanding (and I'm not an expert in Angular rendering performance) is that going from hidden -> shown isn't much different starting as shown to begin with. You still need to show the content and you aren't paying much of a cost by rendering nothing first (not sure if that waits a frame though).

    By contract, going from shown -> hidden does unnecessary rendering work as opposed to just starting hidden. Users have to pay the cost of rendering a piece of DOM that serves no purpose and is immediately removed from the tree. Starting out in the hidden state would skip this work.

    I think in terms of CLS they are equivalent (assuming hidden -> shown does wait a frame), but in terms of work done by the browser a negated async would be worse, because you're wasting rendering effort that didn't need to be done.

    From a slightly more subjective standpoint, as a user I would be more annoyed by a piece of UI that appears and disappears after a single frame, than I would from a piece of UI that simply took an extra frame to load in. That depends on bit on how your layout is structured, but in general I would find shown -> hidden more disruptive as a user.

  7. kirillgroshkov commented on Feb 10, 2022

    @kirillgroshkov

    @dgp1130 , ok, I think I start to agree with your reasoning, thanks for elaborating!

    What is the recommended alternative to "negated async pipe" then?

  8. pascal-puetz commented on Feb 11, 2022

    @pascal-puetz

    Just out of interest: isn't this something that ESLint can already do using the "@angular-eslint/eslint-plugin" (Rule: @angular-eslint/template/no-negated-async )?

  9. dario-piotrowicz commented on Feb 13, 2022

    @dario-piotrowicz
    Contributor

    I'm wondering, does the async pipe need to emit null before checking the condition? 🤔

    Could for example a startWith-like argument be added to the pipe instead? so that the developers can put the initial value there which will be used straight away before the condition checking and also the fist emission?

    That could also be a handy shorter way for the developer to start with some value instead of having to add a pipe and startWith to the existing observable 🤔

  10. angular-robot commented on Mar 8, 2022

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  11. angular-robot commented on Mar 28, 2022

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  12. 2 remaining items

  13. mgechev commented on Aug 2, 2022

    @mgechev
    Member

    We found it valuable, but I'd say we have higher-priority checks we can look into (performance and a11y conformance).

  14. added
    feature: under consideration
    and removed on Sep 26, 2022
  15. added 3 commits that reference this issue on Sep 23, 2025
    7ed1eec
    f5c839c
    77b5691
  16. added a commit that references this issue on Oct 4, 2025
    30fc528
  17. added a commit that references this issue on Oct 20, 2025
    be042a4
  18. JeanMeche commented on Oct 20, 2025

    @JeanMeche
    Member

    While Angular fully supports the AsyncPipe, modern patterns are sailing away from it.

    For those that want to cleanup their codebase they can use dedicated eslint rule: @angular-eslint/template/no-negated-async.

  19. angular-automatic-lock-bot commented on Nov 20, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  20. locked and limited conversation to collaborators on Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compilercompiler: extended diagnosticsfeaturefeature: under consideration

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions