I've found it quite "disturbing" in 2026 there are APIs that wouldn't use Promise and accept a last argument as function to retrieve data, see: https://doc.qt.io/qt-6/qtwebchannel-javascript.html#interacting-with-qobjects
Here an easy way to workaround that, where equality of the method is preserved and things would look more modern or easier to reason about.
const { apply } = Reflect;
const { create } = Object;
const wm = new WeakMap;
const promise = fn => function (...args) {
return new Promise((res, rej) => {
args.push(res);
try {
apply(fn, this, args);
}
catch (err) {
rej(err);
}
});
};
const handler = {
get(target, prop) {
const value = target[prop];
if (typeof value === 'function') {
const fns = wm.get(target);
return fns[prop] ?? (fns[prop] = promise(value));
}
return value;
},
};
const qtProxy = target => {
if (!wm.has(target)) wm.set(target, create(null));
return new Proxy(target, handler);
};Using qtProxy(qtReference) would return a proxy able to provide methods that could be simply awaited when needed.
Use the classic method(1, 2, 3).then(result => { ... }) instead when/if awaiting is not desired.
// example
const foo = qtProxy(channel.objects.foo);
const result = await foo.myMethod(arg1, arg2);
console.log(result); // 'done'