Skip to content

remove creating an extra Promise just for common cleanup #4339

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

Merged
merged 2 commits into from
Jul 18, 2025

Conversation

bmeck
Copy link
Member

@bmeck bmeck commented Jul 16, 2025

This relates to...

N/A

Rationale

While debugging some Promise allocation and lack of use I found extra ticks associated with a .catch in undici that are creating a Promise, adopting the result of another promise and then creating 1 extra tick.

Changes

I made a change that removes a closure and extra Promise allocation around fetching => mainFetch. This doesn't fully remove wast as mainFetch is still performing an extra allocation and tick at end of the function that isn't utilized.

I didn't see a direct spec correspondance with the controller being terminated that was the cause of using .catch but left the behavior unchanged except doing it on an earlier tick.

Features

Bug Fixes

Breaking Changes and Deprecations

Status

Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

changes are not matching description

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@bmeck
Copy link
Member Author

bmeck commented Jul 17, 2025

@Uzlopak what doesn't match?

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 17, 2025

I would basically expect an added try catch block, everything inside being just white space changes, because of the intendation changes, but here also comments are changed and behaviour of fetch seems also be modified.

@bmeck
Copy link
Member Author

bmeck commented Jul 17, 2025

Here are 2 dumps of async hooks logs that show the extra allocation and before event (from https://github.com/SocketDev/promise-diagnostics-hook ).

Based upon Node 24.4.1:

let a = (async function () {
    try {
    } catch {}
})();
let b = a//.catch(() => {})
Case init events before events dump
a.catch(...) 66 55 async_hooks.using-promise-method-catch.log
a 64 53 async_hooks.no-method-call.log

@bmeck
Copy link
Member Author

bmeck commented Jul 17, 2025

@Uzlopak there are cases where that catch behavior is not used hence adding a parameter. There are no comment changes. I'm cool not merging this without a little nicer interaction cause i'm not about to spend time arguing a tiny diff.

Here is a screenshot of the github UI showing no comment changes.
Screenshot 2025-07-17 at 09 48 28

@mcollina
Copy link
Member

Tests are unfortunately failing.

@Uzlopak

This comment was marked as outdated.

Copy link
Collaborator

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

github doesn't render the changes very well but I believe I'm making sense of it. all looks good to me to avoid the extra promise

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 17, 2025

OK, i had a look at it at the hotel and i modified it to pass the tests.

@mcollina mcollina requested a review from Uzlopak July 18, 2025 07:41
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina
Copy link
Member

@Uzlopak can you approve then?

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 18, 2025

When i pushed I revoked my review as i modified the code myself. I approve if you like, no problem.

The merge button is not green because of a failing test in windows. I checked but it doesnt show why.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 18, 2025

Ah! It passed now.

@bmeck
I hope you know that i didnt meant to nitpick you, as you could see from my Screenshot it was irritating.

Also i thank you personally. That .catch() was actually annoying me. I personally think that we should remove as much as possible the async operators from our code in fetch. The whole spec indicates that it is actually a callback based.

@Uzlopak Uzlopak merged commit b6dc60e into nodejs:main Jul 18, 2025
62 of 64 checks passed
@github-actions github-actions bot mentioned this pull request Jul 18, 2025
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.

5 participants