Here's a minimal example:
const proxy = new Proxy({}, {
get (target, name) {
console.error('Getter ' + name + ' should not be called')
}
});
const promise = Promise.resolve(proxy);
promise.then((proxy) => proxy);
In my mind, the call to then
is accessing the property on the Promise
, and so the getter on Proxy
should never be accessed in the above example. Is this expected behaviour, or a bug? And, if it is expected, why?
promise.then on its own is just fine, and is not invoking the proxy. For example, add in the following line of code, and you won't see an additional use of the proxy:
Instead, this comes down to the specification for promises. When you call
resolve(proxy)
, part of the promise resolution procedure includes checking the value ofproxy.then
. Then based on whether proxy.then is a function or not, it affects how the resolution process continues. Since proxy.then is being accessed, you're seeing the log statements.See section 2.3 of the Promise A+ spec, in particular subsection 2.3.3.1