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
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