JavaScriptFundamental (ES6), part 2
Why a Proxy get trap can break await in JavaScript
62 A get trap that fires for `then` and breaks await
hardawait x reads x.then. So a proxy with a catch-all get trap can accidentally become a thenable. Show that awaiting a proxy whose trap returns a non-function is harmless, that a trap returning a function for every key makes the promise machinery call then, and that the promise then never settles.
Stuck? Three hints, smallest first
- 1. Anything is a thenable if
typeof x.then === 'function'. Nothing else is checked. - 2.
Promise.resolve(thenable)callsthen(resolve, reject)on it in a microtask. - 3. Do not await that promise. Race it against one that settles, or the panel waits for ever.
const thenCalls = [];
// A trap that answers every unknown key with a string. 'then' is a string, so
// the proxy is not a thenable, and await hands it straight back.
const withString = new Proxy({ receipt: 'LAG-0062' }, {
get: (t, k, r) => (Reflect.has(t, k) ? Reflect.get(t, k, r) : 'unknown field'),
});
const awaited = await withString;
const sameObject = awaited === withString;
const readable = awaited.receipt;
const thenIsAString = typeof withString.then;
// The same trap, answering with a function. Now it looks like a promise.
const withFunction = new Proxy({ receipt: 'LAG-0062' }, {
get: (t, k, r) => (Reflect.has(t, k) ? Reflect.get(t, k, r)
: (...args) => { thenCalls.push([String(k), args.length, typeof args[0], typeof args[1]]); }),
});
const looksThenable = typeof withFunction.then === 'function';
thenCalls.length = 0;
const adopted = Promise.resolve(withFunction); // NOT awaited: it can never settle
await Promise.resolve(); // let the thenable job run
await Promise.resolve();
const whatThenGot = thenCalls.slice();
const raced = await Promise.race([adopted.then(() => 'settled'), Promise.resolve('still pending')]);
// The fix: answer 'then' honestly, whatever else the trap invents.
const safe = new Proxy({ receipt: 'LAG-0062' }, {
get(t, k, r) {
if (k === 'then' || k === 'catch' || k === 'finally') return Reflect.get(t, k, r);
return Reflect.has(t, k) ? Reflect.get(t, k, r) : 'unknown field';
},
});
const safeAwait = await safe;
const safeIsSame = [safeAwait === safe, safeAwait.receipt, typeof safe.then];
console.log('await a non-thenable proxy:', sameObject, readable);
console.log('then was called with :', whatThenGot);
console.log('race says :', raced);
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('a trap returning a non-function for then leaves the proxy non-thenable', thenIsAString, 'string');
test('so await gives back the very same object', [sameObject, readable], [true, 'LAG-0062']);
test('a trap returning a function for every key makes it look like a promise', looksThenable, true);
test('and the machinery calls then, handing it resolve and reject', whatThenGot, [['then', 2, 'function', 'function']]);
test('that then resolves nothing, so the promise never settles', raced, 'still pending');
test('answering then from the target only keeps await working', safeIsSame, [true, 'LAG-0062', 'undefined']);
test('nothing else about the trap changed: unknown keys still get the default', safe.zone, 'unknown field');
test('and thenCalls proves the safe proxy was never asked to be a promise', thenCalls.length, 1);
There is no interface to implement and no marker to set. await and then ask one question — is x.then callable — and any object that answers yes is treated as a promise. A catch-all get trap answers yes by accident, and the failure is the worst kind: await proxy hangs for ever with no error, no stack and nothing logged.
The fourth assertion shows what the runtime actually does. It calls then.call(proxy, resolve, reject) in a microtask and waits. A default handler that ignores both arguments never calls either one, so the promise stays pending until the process exits. That is why the race in the fifth assertion is here at all: it is the only way to assert "never settles" without hanging the build.
Two rules follow. If you write a catch-all trap, exempt then — and while you are there, catch, finally and Symbol.toPrimitive, which the same accident affects. And when a default value must be invented, prefer undefined or a non-callable: it is the callability that does the damage. The same trap bites plain objects too, which is why an ORM row or a mock whose every property is a stub function should never be returned from an async function.