Skip to content

fix(zone.js): patch Response methods returned by fetch#50653

Closed
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/50327
Closed

fix(zone.js): patch Response methods returned by fetch#50653
arturovt wants to merge 1 commit intoangular:mainfrom
arturovt:fix/50327

Conversation

@arturovt
Copy link
Copy Markdown
Contributor

@arturovt arturovt commented Jun 10, 2023

This commit updates the implementation of the fetch patch and additionally patches Response methods which return promises. These are arrayBuffer, blob, formData, json and text. This fixes the issue when zone becomes stable too early before all of the fetch tasks complete. Given the following code:

appRef.isStable.subscribe(console.log);
fetch(...).then(response => response.json()).then(console.log);

The isStable observer would log false, true, false, true. This was happening because json() was returning a native promise (and not a ZoneAwarePromise). But calling then on the native promise returns a ZoneAwarePromise which notifies Angular about the task being scheduled and forces to re-calculate the isStable state.

Closes: #50327

@pullapprove pullapprove Bot requested a review from JiaLiPassion June 10, 2023 21:33
@AndrewKushnir AndrewKushnir added action: review The PR is still awaiting reviews from at least one requested reviewer area: zones Issues related to zone.js area: server Issues related to server-side rendering target: patch This PR is targeted for the next patch release labels Jun 11, 2023
@ngbot ngbot Bot added this to the Backlog milestone Jun 11, 2023
Copy link
Copy Markdown
Contributor

@JiaLiPassion JiaLiPassion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit updates the implementation of the `fetch` patch and additionally
patches `Response` methods which return promises. These are `arrayBuffer`, `blob`,
`formData`, `json` and `text`. This fixes the issue when zone becomes stable too early
before all of the `fetch` tasks complete. Given the following code:
```ts
appRef.isStable.subscribe(console.log);
fetch(...).then(response => response.json()).then(console.log);
```
The `isStable` observer would log `false, true, false, true`. This was happening because
`json()` was returning a native promise (and not a `ZoneAwarePromise`). But calling `then`
on the native promise returns a `ZoneAwarePromise` which notifies Angular about the task
being scheduled and forces to re-calculate the `isStable` state.

Issue: angular#50327
Copy link
Copy Markdown
Contributor

@JiaLiPassion JiaLiPassion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks.

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit action: global presubmit The PR is in need of a google3 global presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer action: presubmit The PR is in need of a google3 presubmit labels Jan 31, 2024
@AndrewKushnir
Copy link
Copy Markdown
Contributor

Presubmit (TGP).

@AndrewKushnir AndrewKushnir added action: merge The PR is ready for merge by the caretaker and removed action: global presubmit The PR is in need of a google3 global presubmit labels Jan 31, 2024
jessicajaniuk pushed a commit that referenced this pull request Jan 31, 2024
This commit updates the implementation of the `fetch` patch and additionally
patches `Response` methods which return promises. These are `arrayBuffer`, `blob`,
`formData`, `json` and `text`. This fixes the issue when zone becomes stable too early
before all of the `fetch` tasks complete. Given the following code:
```ts
appRef.isStable.subscribe(console.log);
fetch(...).then(response => response.json()).then(console.log);
```
The `isStable` observer would log `false, true, false, true`. This was happening because
`json()` was returning a native promise (and not a `ZoneAwarePromise`). But calling `then`
on the native promise returns a `ZoneAwarePromise` which notifies Angular about the task
being scheduled and forces to re-calculate the `isStable` state.

Issue: #50327

PR Close #50653
@jessicajaniuk
Copy link
Copy Markdown
Contributor

This PR was merged into the repository by commit 260d3ed.

@arturovt arturovt deleted the fix/50327 branch January 31, 2024 18:01
@angular-automatic-lock-bot
Copy link
Copy Markdown

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.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: server Issues related to server-side rendering area: zones Issues related to zone.js target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fetch only partially patched.

4 participants