October 26, 2024
Chicago 12, Melborne City, USA
javascript

Promise redefinition does not affect promises returned by native functions


(See also this answer.)

When I redefine the Promise class (with a "monkey-patch" as shown below), this does not affect promises that are returned by a native function like fetch:

Promise = class extends Promise {
  constructor(executor) {
    console.log("Promise created");
    super(executor);
  }
}
console.log("Non-native promise");
Promise.resolve(1);
console.log("Native promise");
fetch("https://httpbin.org/delay/1");
console.log("Native promise");
document.hasStorageAccess();

When I run this snippet in the browser, the promise created by Promise.resolve uses the redefinition (so that "Promise created" is logged), but the promises returned by fetch or document.hasStorageAccess do not.

(When I try it in Node.js, fetch uses the redefinition, so it really depends on the implementation.)

I there a way to redefine Promise such that the redefinition is also used by native browser functions?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video