In Proxy how to tell if user is expecting promise?

42 Views Asked by At

I have setup a proxy to "promisify" callbacked functions like this:

const browser = new Proxy({}, {
    get: function(target, name, receiver) {
        return function(...args) {
            if (userExpectsPromise) { // how to detect this?
                return new Promise(resolve => chrome[name](...args, resolve))
            } else {
                return chrome[name](...args);
            }
        };
        return target[name];
    }
});

What this does is allows user to do browser.***('arg1', 'arg2'), this will call *** on the chrome branch.

However some of the *** on chrome branch accept a final argument of a callback. When it does, a user adds a .then so they would do - browser.***('arg1', 'arg2').then(.....). I need to detect/intercept if user did a .then. Is this possible?

My other option would be to hardcode which chrome.*** accept a callback, but that is not safe way, as the chrome.*** API is always evolving.

0

There are 0 best solutions below