diagnostics_channel: return original thenable#62407
Conversation
This makes tracePromise return the original thenable to allow custom thenable types to retain their methods rather than producing the chained result type.
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.68% 89.69%
=======================================
Files 676 676
Lines 206710 206720 +10
Branches 39584 39585 +1
=======================================
+ Hits 185398 185417 +19
- Misses 13441 13442 +1
+ Partials 7871 7861 -10
🚀 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()?
| // TODO(qard): Not realm safe? Should be a construct.name check? | ||
| if (result.constructor === Promise) { |
There was a problem hiding this comment.
require('internal/util/types').isPromise()?
There was a problem hiding this comment.
That is not equivalent. It needs to only match types which are directly native promises, not sub-classes. This is why it checks that the direct constructor is Promise rather than doing something like an instanceof check.
|
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. |
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