> > const goodPromises = new WeakSet(); > class DefensivePromise { > constructor(x) { > super(x); > if (new.target === DefensivePromise) { > Object.freeze(this); > goodPromises.add(this); > } > } > static resolve(x) { > if (goodPromises.has(x)) { > return x; // should be equiv to super.resolve(x); > } > return new DefensivePromise(r => {r(x)}); > } > } > > Basically you can't rely on new.target to mean what you think it means, in the face of Reflect.construct.
Maybe this? constructor(x) { super(x); // At this point you know that "this" is a Promise, but you don't // know if the prototype is set correctly, so: if (Object.getPrototypeOf(this) === DefensivePromise.prototype) gooPromises.add(Object.freeze(this)); }
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss