diagnostics_channel: return original thenable#62407
Conversation
fc213c4 to
3eb07ea
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #62407 +/- ##
=======================================
Coverage 89.77% 89.77%
=======================================
Files 697 697
Lines 215773 215782 +9
Branches 41304 41300 -4
=======================================
+ Hits 193704 193713 +9
+ Misses 14162 14158 -4
- Partials 7907 7911 +4
🚀 New features to boost your workflow:
|
Renegade334
left a comment
There was a problem hiding this comment.
Does this change have a specific case in mind? The A+ standard mandates .then() to return another "promise" instance (either the same object or a new object at the implementation's discretion), so any custom features of a compliant thenable should also be present on the object returned by .then()?
|
No, that is not the case. A thenable can return any variety of promise, not necessarily another of itself. It's most common that a thenable returns native promises from its then/catch, so you lose access to whatever methods it had when following the chain. |
3eb07ea to
3d95103
Compare
3d95103 to
c5fcc50
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
81fd7b1 to
3299411
Compare
|
Nit: also having |
3299411 to
b0e835c
Compare
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
b0e835c to
4bae14a
Compare
e2f3d29 to
4b52fa7
Compare
|
I ran into this while working on TracingChannel proposals for several libraries. I think Prisma and Prisma Client operations return a lazy thenable with fluent relation-chaining methods (e.g. then(onFulfilled, onRejected) {
return _callback().then(onFulfilled, onRejected)
}So when tracePromise chains .then() on it, the return value is a native Promise and all PrismaPromise-specific methods are lost: const query = operationChannel.tracePromise(
() => prisma.user.findUnique({ where: { id: 1 } }),
context
);
// query is now a native Promise
// query.posts() → TypeError: query.posts is not a function
// query.requestTransaction() → goneA potential workaround is to avoid We also hit this in the TracingChannel PR for node-postgres (brianc/node-postgres#3650). pg supports a user-configurable Promise constructor via https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/client.js#L12. Which again was worked around with |
This makes tracePromise return the original thenable to allow custom thenable types to retain their methods rather than producing the chained result type.
4b52fa7 to
3522e89
Compare
| // For non-native thenables, subscribe to the result but return the | ||
| // original thenable so the consumer can continue handling it directly. | ||
| // Non-native thenables don't have unhandledRejection tracking, so | ||
| // swallowing the rejection here doesn't change existing behaviour. | ||
| result.then(onResolve, onReject); |
There was a problem hiding this comment.
This doesn't apply to custom native Promises, so these cases probably need to be handled differently.
process.on('unhandledRejection', e => console.error('Unhandled rejection:', e))
class MyPromise extends Promise {}
new MyPromise((resolve, reject) => reject(new Error()))
// Unhandled rejection: Error
// at ...Could either abort the prototype check and just return result.then(onResolve, onRejectWithRethrow) for all native Promises, or keep the PromisePrototypeThen primordial path for vanilla Promises and have a result.then()-returning path for native Promises that fail the prototype check.
There was a problem hiding this comment.
That's intentional. Non-extended native promises are the only variety of promise that you can safely know that chaining will not break the type contract. The specific problem this is trying to address is that many custom promise types are (wrongly) created by sub-classing Promise but then overriding the then(...) method to return a different promise, orphaning the sub-classed promise instance in the process. Both OpenAI and Anthropic API clients do this. I'm pretty sure at least one of the API client generation as a product vendors are actually doing this and so this pattern exists in many, many API client libraries.
This seems to me to be the least-bad approach as there's not really any reason one would sub-class a promise unless they intended to actually do something custom with it, in which case we should be returning that custom thing or we are breaking their API contract.
This makes tracePromise return the original thenable to allow custom thenable types to retain their methods rather than producing the chained result type.
This branches the behaviour between native promises and thenables such that native promises retain the correct unhandledRejection behaviour while thenables produce the correct original return value and so retain methods present on that type.
I think that because native promises are consistently shaped it doesn't actually matter that it remains chained rather than branched.
cc @nodejs/diagnostics