SubTyping Promise?
# Allen Wirfs-Brock (11 years ago)
Don't know about the current V8 inpl, but this should work per the ES6 spec:
class MyPromiseType extends Promise {
constructor() {
super((f,t) => this.f=f, this.t=t)
}
}
let my = new MyPromise;
The magic is in the Promise[Symbol.create]
method which is inherited by MyPromise
and invoked during (new MyPromise)
. Playing around with the instance side prototype chain enough enough.
# Allen Wirfs-Brock (11 years ago)
err, missing word
The magic is in the
Promise[Symbol.create]
method which is inherited byMyPromise
and invoked during(new MyPromise)
. Playing around with the instance side prototype chain >>isn't<< enough.
# Domenic Denicola (11 years ago)
In practice, this means that (sticking to ES5 syntax), you need to do
MyPromise.__proto__ = Promise;
# Allen Wirfs-Brock (11 years ago)
Yes, but it also depends upon Promise[Symbol.create] being implemented.
# Andreas Rossberg (11 years ago)
Right. V8 has not yet implemented @@create yet (and it will likely take some time until it does, since it's a rather intrusive feature).
Are we able to ensure that Promises can be sub-typed right now? Did not see things in the spec about this.
In Chome/V8:
function MyPromiseType() { var self=this; Promise.call(this, function (f,r) { self.f = f; self.r = r; }); } MyPromiseType.prototype = Object.create(Promise.prototype); new MyPromiseType()
And if we do sub-type a promise, that seems to be the only way to hook into state propagation for promises (according to A+). This means that anything wishing to have state propagation must have Promise in the prototype chain. This seems fine to me, but means modifying existing prototype chains must start from the beginning of the chain rather than being able to add a Promise interface on top of the end of the chain.
For example with the prototype chain:
Game -> CardGame -> BlackJackGame
We could not turn the BlackJack type into a promise correct? Just trying to understand at this point.