In practice, I think this is very hard to explain out there:
const sideEffect = {
get value() {
return Math.random();
}
};
class Sub extends Promise {
constructor(callback) {
// expected callback invoke *once* up there
super((resolve, reject) => {
console.log(sideEffect.value);
callback(resolve, reject);
});
}
}
class SubSub extends Sub {
constructor(callback) {
// expected callback invoke *once* up there
// because super calls it once, expected max 2 invokes
super((resolve, reject) => {
console.log(sideEffect.value);
callback(resolve, reject);
});
}
}
// expected 2 Math.random()
new SubSub(Object).then(Object) instanceof SubSub;
// true ... but ...
// got 4 Math.random()
// 0.8782228881162686
// 0.6226237763466325
// 0.8762779980398552
// 0.6806218405445226
As summary: shouldn't the constructor callback, for same constructor that derives its own thenable, trigger the callback never more than once? If not, what's the practical use case that supports current state of affairs? and why is defaulting to the super species is the only way to solve this?
Moreover: why a then() should involve its constructor ever, when it's a branched derived promise of what its constructor did once?