Remove Promise monkey-patching

This commit is contained in:
Fabio Massaioli 2025-02-25 17:53:18 +01:00
commit 5b2c8eba80

View file

@ -386,7 +386,7 @@ export class CancellablePromise<T> extends Promise<T> implements PromiseLike<T>,
this[barrierSym] = barrier;
return new CancellablePromise<TResult1 | TResult2>((resolve, reject) => {
void promiseThen.call(this,
void super.then(
(value) => {
if (this[barrierSym] === barrier) { this[barrierSym] = null; }
barrier.resolve?.();
@ -717,7 +717,7 @@ function cancellerFor<T>(promise: CancellablePromiseWithResolvers<T>, state: Can
// In theory, a sane underlying implementation at this point
// should always reject with our cancellation reason,
// hence the handler will never throw.
void promiseThen.call(promise.promise, undefined, (err) => {
void Promise.prototype.then.call(promise.promise, undefined, (err) => {
if (err !== reason) {
throw err;
}
@ -920,16 +920,6 @@ function currentBarrier<T>(promise: CancellablePromise<T>): Promise<void> {
return pwr.promise!;
}
// Stop sneaky people from breaking the barrier mechanism.
const promiseThen = Promise.prototype.then;
Promise.prototype.then = function(...args) {
if (this instanceof CancellablePromise) {
return this.then(...args);
} else {
return Reflect.apply(promiseThen, this, args);
}
}
// Polyfill Promise.withResolvers.
let promiseWithResolvers = Promise.withResolvers;
if (promiseWithResolvers && typeof promiseWithResolvers === 'function') {