fix: Preserve Promise subclass return values in wrapPromise#57
Merged
fix: Preserve Promise subclass return values in wrapPromise#57
Conversation
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>
1374e7a to
0567506
Compare
bizob2828
approved these changes
Apr 6, 2026
Contributor
bizob2828
left a comment
There was a problem hiding this comment.
I verified this solves our issue with openai, antthropic as well as pg where we are manually wrapping promise in a traceSync call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a function returns a Promise subclass (e.g. Anthropic SDK's
APIPromise, which adds a.withResponse()method), the previouswrapPromisetemplate returnedpromise.then(cb), which replaces the original with a plainPromiseand loses any methods on the subclass.The fix follows Node.js core
diagnostics_channelbehaviour: chain normally for nativePromiseinstances (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_subclasstest to cover this case.