Skip to content

fix: Preserve Promise subclass return values in wrapPromise#57

Merged
bizob2828 merged 1 commit intomainfrom
fix/preserve-original-promise-return-value
Apr 6, 2026
Merged

fix: Preserve Promise subclass return values in wrapPromise#57
bizob2828 merged 1 commit intomainfrom
fix/preserve-original-promise-return-value

Conversation

@Qard
Copy link
Copy Markdown
Member

@Qard Qard commented Apr 1, 2026

When a function returns a Promise subclass (e.g. Anthropic SDK's APIPromise, which adds a .withResponse() method), the previous wrapPromise template returned promise.then(cb), which replaces the original with a plain Promise and loses any methods on the subclass.

The fix follows Node.js core diagnostics_channel behaviour: chain normally for native Promise instances (safe, nothing to lose), and for subclasses or other thenables, side-chain .then() for the async channel callbacks and return the original promise unchanged.

Adds a promise_subclass test to cover this case.

When a function returns a Promise subclass (e.g. Anthropic SDK's
APIPromise, which adds a .withResponse() method), returning
promise.then(cb) replaces it with a plain Promise, losing any
methods on the subclass.

Following Node.js core diagnostics_channel behaviour: chain normally
for native Promise instances, and for subclasses or other thenables,
side-chain .then() for the async channel callbacks and return the
original promise.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Qard Qard force-pushed the fix/preserve-original-promise-return-value branch from 1374e7a to 0567506 Compare April 1, 2026 01:36
@Qard Qard changed the title fix: Preserve original return value in wrapPromise to avoid losing Promise subclasses fix: Preserve Promise subclass return values in wrapPromise Apr 1, 2026
@Qard Qard requested a review from rochdev April 2, 2026 21:14
Copy link
Copy Markdown
Contributor

@bizob2828 bizob2828 left a comment

Choose a reason for hiding this comment

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

I verified this solves our issue with openai, antthropic as well as pg where we are manually wrapping promise in a traceSync call

@bizob2828 bizob2828 merged commit d59a4e6 into main Apr 6, 2026
6 checks passed
@jsumners-nr jsumners-nr deleted the fix/preserve-original-promise-return-value branch April 6, 2026 19:20
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.

2 participants