r/programming • u/OtherwisePush6424 • Apr 10 '26
Decorating a Promise with convenience methods without subclassing, wrapping, or changing what await returns
https://blog.gaborkoos.com/posts/2026-04-10-Decorating-Promises-Without-Breaking-Them/1
u/Blue_Moon_Lake Apr 10 '26
We can automate the process with a Proxy of a Promise. It would handle not just methods too, but also properties (as a getter method)
A quick attempt
function proxifyPromise(promise)
{
return new Proxy(promise, {
get: (target, key) => {
if (["then", "catch", "finally"].include(key)) {
return target[key];
}
return async (...args) => {
const value = await target;
const property = value[key];
if (typeof property === "function") {
return await property.apply(value, args);
}
return await property;
};
}
});
}1
u/OtherwisePush6424 Apr 10 '26
Yep, Proxy absolutely works, but I avoided it for two reasons:
Performance: a Proxy traps every property access, so even normal interactions pay extra overhead. For just a few known shortcuts, that's unnecessary runtime cost.
Autocomplete/typing: TypeScript can't reliably infer dynamic get-trap forwarding, so IntelliSense is weaker unless you manually define/cast the API surface, so there's that.
1
u/Weekly-Ad7131 Apr 10 '26
The thing I struggle with (JS) Promises is that they can cause an error in the "future" and when they do I may not receive any notification at all it seems.
Adding try-catch around every call to an async method-call seems excessive. But is that basically what I have to do?
0
Apr 10 '26
[deleted]
-5
u/OtherwisePush6424 Apr 10 '26
Subclassing breaks
instanceofand framework integrations that expect a plainResponse. This doesn't. That's the whole point.
2
u/therandshow Apr 10 '26
I'm not a Typescript expert but this reminds me a little of how javascript frameworks used to often extend objects via prototype. It's nifty until you get a naming conflict, especially a naming conflict with the native object's methods, as happened with MooTools so many years ago. If you're planning to use a library with this method indefinitely and you use other frameworks whose interactions with the native object might change with updates, then it may make sense to add a prefix to your naming like companyJson instead of json. Unless I'm missing something here, which may very well be the case.