This repository was archived by the owner on Jan 26, 2022. It is now read-only.
Explicit species constructor logic unnecessary? #48
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.
In order to be tolerant of
Promise
subclassing, polyfill implementations ofPromise.prototype.finally
typically implement logic to obtain the species constructorC
, which is then used to callC.resolve(onFinally())
ornew C(resolve => resolve(onFinally())
to ensure the resolved promise isinstanceof
the custom subclass.However, a polyfill implementation of
Promise.prototype.finally
that does not need to callresolve
explicitly could potentially avoid this extra effort to obtain the appropriate species constructor. If this new implementation is truly equivalent (modulo any fixable problems that I may have overlooked), then spec-compliant polyfills would no longer incur a penalty for following the spec exactly, compared to naive implementations that ignore these details for the sake of simplicity.This pull request demonstrates one such implementation. Here's the essential logic:
The trick is to return the result of
onFinally()
from the.then
callback functions, which automatically resolves the return value to an instance of the currentPromise
subclass, using whatever resolution logic the subclass implements. The final.then
will be invoked against an instance of the subclass, allowing the subclass to define the behavior of.then
and thus the final result of the.finally
method.Note that a spec-compliant implementation of
Promise.prototype.then
will almost certainly still need to obtain the species constructor in its own implementation. However, it seems desirable to keep that logic out ofPromise.prototype.finally
if possible.After considering this implementation, if you still believe the explicit species constructor logic is necessary, I would encourage you to write a test that demonstrates the difference.
If there are no objections to this new implementation, this repository seems like the appropriate place to provide updated guidance to other implementations. In other words, if this PR is merged, I will gladly submit PRs to other implementations, referring back to this PR.